From 07ba6fbe265f512cce2714bf81263e613414fa10 Mon Sep 17 00:00:00 2001 From: Chengxiong Ruan Date: Wed, 11 Jan 2023 21:47:04 -0500 Subject: [PATCH] sql/schemachanger: declarative drop function 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 --- .../backup-restore/user-defined-functions | 6 +- .../backup_base_generated_test.go | 5 + .../drop_database_multiregion_primary_region | 4 +- .../end_to_end/drop_table_multiregion | 4 +- .../drop_table_multiregion_primary_region | 4 +- .../drop_database_multiregion_primary_region | 10 +- .../testdata/explain/drop_table_multiregion | 6 +- .../drop_table_multiregion_primary_region | 6 +- .../drop_database_multiregion_primary_region | 28 +- .../explain_verbose/drop_table_multiregion | 12 +- .../drop_table_multiregion_primary_region | 12 +- pkg/sql/catalog/schemadesc/schema_desc.go | 2 +- pkg/sql/drop_function_test.go | 35 +- pkg/sql/schemachanger/scbuild/BUILD.bazel | 1 + .../schemachanger/scbuild/builder_state.go | 39 ++ pkg/sql/schemachanger/scbuild/event_log.go | 4 + .../scbuild/internal/scbuildstmt/BUILD.bazel | 1 + .../scbuildstmt/alter_table_drop_column.go | 15 + .../internal/scbuildstmt/dependencies.go | 3 + .../internal/scbuildstmt/drop_function.go | 38 ++ .../internal/scbuildstmt/drop_index.go | 30 ++ .../scbuild/internal/scbuildstmt/helpers.go | 17 + .../scbuild/internal/scbuildstmt/process.go | 1 + .../scbuild/testdata/drop_function | 43 ++ .../schemachanger/scdecomp/testdata/function | 4 +- .../scdeps/sctestdeps/BUILD.bazel | 1 + .../scdeps/sctestdeps/test_deps.go | 53 ++- .../scexec/scmutationexec/references.go | 22 +- .../schemachanger/scop/immediate_mutation.go | 6 + .../immediate_mutation_visitor_generated.go | 6 + .../scplan/internal/opgen/BUILD.bazel | 7 + .../scplan/internal/opgen/opgen_function.go | 48 +++ .../internal/opgen/opgen_function_body.go | 60 +++ .../opgen/opgen_function_leakproof.go | 37 ++ .../internal/opgen/opgen_function_name.go | 37 ++ .../opgen/opgen_function_null_input.go | 37 ++ .../opgen/opgen_function_param_default.go | 39 ++ .../opgen/opgen_function_volatility.go | 37 ++ .../internal/opgen/opgen_object_parent.go | 6 + .../scplan/internal/rules/current/helpers.go | 7 +- .../scplan/internal/rules/current/op_drop.go | 4 + .../internal/rules/current/testdata/deprules | 20 +- .../internal/rules/current/testdata/oprules | 6 +- .../scplan/testdata/alter_table_drop_column | 28 +- .../scplan/testdata/drop_database | 90 ++++- .../scplan/testdata/drop_function | 169 ++++++++ .../schemachanger/scplan/testdata/drop_index | 28 +- .../scplan/testdata/drop_owned_by | 60 ++- .../schemachanger/scplan/testdata/drop_schema | 78 +++- .../scplan/testdata/drop_sequence | 20 +- .../schemachanger/scplan/testdata/drop_table | 36 +- .../schemachanger/scplan/testdata/drop_type | 32 +- .../schemachanger/scplan/testdata/drop_view | 68 +++- pkg/sql/schemachanger/screl/attr.go | 21 + pkg/sql/schemachanger/screl/scalars.go | 4 +- pkg/sql/schemachanger/sctest/cumulative.go | 1 + .../schemachanger/sctest_generated_test.go | 25 ++ .../testdata/end_to_end/drop_function | 382 ++++++++++++++++++ .../drop_index_with_materialized_view_dep | 4 +- .../testdata/end_to_end/drop_table | 4 +- .../testdata/explain/drop_function | 94 +++++ .../drop_index_with_materialized_view_dep | 8 +- .../schemachanger/testdata/explain/drop_table | 6 +- .../testdata/explain_verbose/drop_function | 374 +++++++++++++++++ .../drop_index_with_materialized_view_dep | 16 +- .../testdata/explain_verbose/drop_table | 12 +- 66 files changed, 2166 insertions(+), 157 deletions(-) create mode 100644 pkg/sql/schemachanger/scbuild/internal/scbuildstmt/drop_function.go create mode 100644 pkg/sql/schemachanger/scbuild/testdata/drop_function create mode 100644 pkg/sql/schemachanger/scplan/internal/opgen/opgen_function.go create mode 100644 pkg/sql/schemachanger/scplan/internal/opgen/opgen_function_body.go create mode 100644 pkg/sql/schemachanger/scplan/internal/opgen/opgen_function_leakproof.go create mode 100644 pkg/sql/schemachanger/scplan/internal/opgen/opgen_function_name.go create mode 100644 pkg/sql/schemachanger/scplan/internal/opgen/opgen_function_null_input.go create mode 100644 pkg/sql/schemachanger/scplan/internal/opgen/opgen_function_param_default.go create mode 100644 pkg/sql/schemachanger/scplan/internal/opgen/opgen_function_volatility.go create mode 100644 pkg/sql/schemachanger/scplan/testdata/drop_function create mode 100644 pkg/sql/schemachanger/testdata/end_to_end/drop_function create mode 100644 pkg/sql/schemachanger/testdata/explain/drop_function create mode 100644 pkg/sql/schemachanger/testdata/explain_verbose/drop_function diff --git a/pkg/ccl/backupccl/testdata/backup-restore/user-defined-functions b/pkg/ccl/backupccl/testdata/backup-restore/user-defined-functions index 8c5131fd28a8..f71274a81483 100644 --- a/pkg/ccl/backupccl/testdata/backup-restore/user-defined-functions +++ b/pkg/ccl/backupccl/testdata/backup-restore/user-defined-functions @@ -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 @@ -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 diff --git a/pkg/ccl/schemachangerccl/backup_base_generated_test.go b/pkg/ccl/schemachangerccl/backup_base_generated_test.go index 9410194ff93e..f3df746c6acd 100644 --- a/pkg/ccl/schemachangerccl/backup_base_generated_test.go +++ b/pkg/ccl/schemachangerccl/backup_base_generated_test.go @@ -103,6 +103,11 @@ func TestBackup_base_drop_column_with_index(t *testing.T) { defer log.Scope(t).Close(t) sctest.Backup(t, "pkg/sql/schemachanger/testdata/end_to_end/drop_column_with_index", newCluster) } +func TestBackup_base_drop_function(t *testing.T) { + defer leaktest.AfterTest(t)() + defer log.Scope(t).Close(t) + sctest.Backup(t, "pkg/sql/schemachanger/testdata/end_to_end/drop_function", newCluster) +} func TestBackup_base_drop_index_hash_sharded_index(t *testing.T) { defer leaktest.AfterTest(t)() defer log.Scope(t).Close(t) diff --git a/pkg/ccl/schemachangerccl/testdata/end_to_end/drop_database_multiregion_primary_region b/pkg/ccl/schemachangerccl/testdata/end_to_end/drop_database_multiregion_primary_region index 69056ea0e6ed..7df3e7d67496 100644 --- a/pkg/ccl/schemachangerccl/testdata/end_to_end/drop_database_multiregion_primary_region +++ b/pkg/ccl/schemachangerccl/testdata/end_to_end/drop_database_multiregion_primary_region @@ -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 @@ -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 diff --git a/pkg/ccl/schemachangerccl/testdata/end_to_end/drop_table_multiregion b/pkg/ccl/schemachangerccl/testdata/end_to_end/drop_table_multiregion index 9adfa7d904e4..5d4e179c8bbc 100644 --- a/pkg/ccl/schemachangerccl/testdata/end_to_end/drop_table_multiregion +++ b/pkg/ccl/schemachangerccl/testdata/end_to_end/drop_table_multiregion @@ -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 ... @@ -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: diff --git a/pkg/ccl/schemachangerccl/testdata/end_to_end/drop_table_multiregion_primary_region b/pkg/ccl/schemachangerccl/testdata/end_to_end/drop_table_multiregion_primary_region index c040da4c5e1d..85ed0c1eb7d8 100644 --- a/pkg/ccl/schemachangerccl/testdata/end_to_end/drop_table_multiregion_primary_region +++ b/pkg/ccl/schemachangerccl/testdata/end_to_end/drop_table_multiregion_primary_region @@ -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 ... @@ -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: diff --git a/pkg/ccl/schemachangerccl/testdata/explain/drop_database_multiregion_primary_region b/pkg/ccl/schemachangerccl/testdata/explain/drop_database_multiregion_primary_region index d73e4e4abc8e..d6d7b53b709a 100644 --- a/pkg/ccl/schemachangerccl/testdata/explain/drop_database_multiregion_primary_region +++ b/pkg/ccl/schemachangerccl/testdata/explain/drop_database_multiregion_primary_region @@ -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"}} @@ -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"}} diff --git a/pkg/ccl/schemachangerccl/testdata/explain/drop_table_multiregion b/pkg/ccl/schemachangerccl/testdata/explain/drop_table_multiregion index e81881b49b92..3d8c527bc787 100644 --- a/pkg/ccl/schemachangerccl/testdata/explain/drop_table_multiregion +++ b/pkg/ccl/schemachangerccl/testdata/explain/drop_table_multiregion @@ -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}} @@ -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}} diff --git a/pkg/ccl/schemachangerccl/testdata/explain/drop_table_multiregion_primary_region b/pkg/ccl/schemachangerccl/testdata/explain/drop_table_multiregion_primary_region index bf8a56d52f8f..9512d93b0ecd 100644 --- a/pkg/ccl/schemachangerccl/testdata/explain/drop_table_multiregion_primary_region +++ b/pkg/ccl/schemachangerccl/testdata/explain/drop_table_multiregion_primary_region @@ -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 @@ -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} diff --git a/pkg/ccl/schemachangerccl/testdata/explain_verbose/drop_database_multiregion_primary_region b/pkg/ccl/schemachangerccl/testdata/explain_verbose/drop_database_multiregion_primary_region index c718614a57ea..2c9075cb2d70 100644 --- a/pkg/ccl/schemachangerccl/testdata/explain_verbose/drop_database_multiregion_primary_region +++ b/pkg/ccl/schemachangerccl/testdata/explain_verbose/drop_database_multiregion_primary_region @@ -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 @@ -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: @@ -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 @@ -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: diff --git a/pkg/ccl/schemachangerccl/testdata/explain_verbose/drop_table_multiregion b/pkg/ccl/schemachangerccl/testdata/explain_verbose/drop_table_multiregion index 4039d1003bd2..757919587dc4 100644 --- a/pkg/ccl/schemachangerccl/testdata/explain_verbose/drop_table_multiregion +++ b/pkg/ccl/schemachangerccl/testdata/explain_verbose/drop_table_multiregion @@ -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 @@ -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 diff --git a/pkg/ccl/schemachangerccl/testdata/explain_verbose/drop_table_multiregion_primary_region b/pkg/ccl/schemachangerccl/testdata/explain_verbose/drop_table_multiregion_primary_region index 3d017a93a10c..64d664f5ba0a 100644 --- a/pkg/ccl/schemachangerccl/testdata/explain_verbose/drop_table_multiregion_primary_region +++ b/pkg/ccl/schemachangerccl/testdata/explain_verbose/drop_table_multiregion_primary_region @@ -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: @@ -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: diff --git a/pkg/sql/catalog/schemadesc/schema_desc.go b/pkg/sql/catalog/schemadesc/schema_desc.go index 05f6cacb0246..11fd1c6d0693 100644 --- a/pkg/sql/catalog/schemadesc/schema_desc.go +++ b/pkg/sql/catalog/schemadesc/schema_desc.go @@ -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) diff --git a/pkg/sql/drop_function_test.go b/pkg/sql/drop_function_test.go index dfd740348b99..ee3032daa94b 100644 --- a/pkg/sql/drop_function_test.go +++ b/pkg/sql/drop_function_test.go @@ -147,7 +147,7 @@ SELECT nextval(105:::REGCLASS);`, err = sql.TestingDescsTxn(ctx, s, func(ctx context.Context, txn isql.Txn, col *descs.Collection) error { _, err := col.ByIDWithLeased(txn.KV()).WithoutNonPublic().Get().Function(ctx, 109) require.Error(t, err) - require.Regexp(t, "descriptor is being dropped", err.Error()) + require.Regexp(t, "function undefined", err.Error()) // Make sure columns and indexes has correct back references. tn := tree.MakeTableNameWithSchema("defaultdb", "public", "t") @@ -197,6 +197,7 @@ CREATE TABLE t( a INT PRIMARY KEY, b INT, C INT, + d INT, INDEX t_idx_b(b), INDEX t_idx_c(c) ); @@ -209,6 +210,7 @@ CREATE FUNCTION test_sc.f(a notmyworkday) RETURNS INT IMMUTABLE LANGUAGE SQL AS SELECT a FROM t; SELECT b FROM t@t_idx_b; SELECT c FROM t@t_idx_c; + SELECT d FROM t; SELECT a FROM v; SELECT nextval('sq1'); $$; @@ -225,20 +227,23 @@ USE defaultdb; tDB.Exec(t, "SET use_declarative_schema_changer = off;") testCases := []struct { - stmt string - expectedErr string + stmt string + expectedErr string + dscExpectedErr string }{ { stmt: "DROP SEQUENCE sq1", expectedErr: "pq: cannot drop sequence sq1 because other objects depend on it", }, { - stmt: "DROP TABLE t", - expectedErr: `pq: cannot drop relation "t" because function "f" depends on it`, + stmt: "DROP TABLE t", + expectedErr: `pq: cannot drop relation "t" because function "f" depends on it`, + dscExpectedErr: `pq: cannot drop table t because other objects depend on it`, }, { - stmt: "DROP VIEW v", - expectedErr: `pq: cannot drop relation "v" because function "f" depends on it`, + stmt: "DROP VIEW v", + expectedErr: `pq: cannot drop relation "v" because function "f" depends on it`, + dscExpectedErr: `pq: cannot drop view v because other objects depend on it`, }, { stmt: "ALTER TABLE t RENAME TO t_new", @@ -249,8 +254,8 @@ USE defaultdb; expectedErr: `pq: cannot set schema on relation "t" because function "f" depends on it`, }, { - stmt: "ALTER TABLE t DROP COLUMN b", - expectedErr: `pq: cannot drop column "b" because function "f" depends on it`, + stmt: "ALTER TABLE t DROP COLUMN d", + expectedErr: `pq: cannot drop column "d" because function "f" depends on it`, }, { stmt: "ALTER TABLE t RENAME COLUMN b TO bb", @@ -296,7 +301,11 @@ USE defaultdb; for i, tc := range testCases { t.Run(strconv.Itoa(i), func(t *testing.T) { _, err := sqlDB.Exec(tc.stmt) - require.Equal(t, tc.expectedErr, err.Error()) + if tc.dscExpectedErr != "" { + require.Equal(t, tc.dscExpectedErr, err.Error()) + } else { + require.Equal(t, tc.expectedErr, err.Error()) + } }) } } @@ -312,6 +321,7 @@ CREATE TABLE t( a INT PRIMARY KEY, b INT, C INT, + d INT, INDEX t_idx_b(b), INDEX t_idx_c(c) ); @@ -324,6 +334,7 @@ CREATE FUNCTION test_sc.f(a notmyworkday) RETURNS INT IMMUTABLE LANGUAGE SQL AS SELECT a FROM t; SELECT b FROM t@t_idx_b; SELECT c FROM t@t_idx_c; + SELECT d FROM t; SELECT a FROM v; SELECT nextval('sq1'); $$; @@ -347,7 +358,7 @@ $$; }, { testName: "drop column", - stmt: "ALTER TABLE t DROP COLUMN b CASCADE", + stmt: "ALTER TABLE t DROP COLUMN d CASCADE", }, { testName: "drop index", @@ -420,7 +431,7 @@ $$; err = sql.TestingDescsTxn(ctx, s, func(ctx context.Context, txn isql.Txn, col *descs.Collection) error { _, err := col.ByIDWithLeased(txn.KV()).WithoutNonPublic().Get().Function(ctx, 113) require.Error(t, err) - require.Regexp(t, "descriptor is being dropped", err.Error()) + require.Regexp(t, "function undefined", err.Error()) return nil }) require.NoError(t, err) diff --git a/pkg/sql/schemachanger/scbuild/BUILD.bazel b/pkg/sql/schemachanger/scbuild/BUILD.bazel index eb3cc0075797..6258aae35fbc 100644 --- a/pkg/sql/schemachanger/scbuild/BUILD.bazel +++ b/pkg/sql/schemachanger/scbuild/BUILD.bazel @@ -23,6 +23,7 @@ go_library( "//pkg/sql/catalog/colinfo", "//pkg/sql/catalog/descpb", "//pkg/sql/catalog/descs", + "//pkg/sql/catalog/funcdesc", "//pkg/sql/catalog/nstree", "//pkg/sql/catalog/resolver", "//pkg/sql/catalog/schemaexpr", diff --git a/pkg/sql/schemachanger/scbuild/builder_state.go b/pkg/sql/schemachanger/scbuild/builder_state.go index 28f642f0ed85..22fc055530ec 100644 --- a/pkg/sql/schemachanger/scbuild/builder_state.go +++ b/pkg/sql/schemachanger/scbuild/builder_state.go @@ -19,6 +19,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/funcdesc" "github.com/cockroachdb/cockroach/pkg/sql/catalog/schemaexpr" "github.com/cockroachdb/cockroach/pkg/sql/catalog/seqexpr" "github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc" @@ -959,6 +960,44 @@ func (b *builderState) ResolveConstraint( }) } +func (b *builderState) ResolveUDF( + fnObj *tree.FuncObj, p scbuildstmt.ResolveParams, +) scbuildstmt.ElementResultSet { + fd, err := b.cr.ResolveFunction(b.ctx, fnObj.FuncName.ToUnresolvedObjectName().ToUnresolvedName(), b.semaCtx.SearchPath) + if err != nil { + if p.IsExistenceOptional && errors.Is(err, tree.ErrFunctionUndefined) { + return nil + } + panic(err) + } + + paramTypes, err := fnObj.ParamTypes(b.ctx, b.cr) + if err != nil { + return nil + } + ol, err := fd.MatchOverload(paramTypes, fnObj.FuncName.Schema(), b.semaCtx.SearchPath) + if err != nil { + if p.IsExistenceOptional && errors.Is(err, tree.ErrFunctionUndefined) { + return nil + } + panic(err) + } + + if !ol.IsUDF { + panic( + errors.Errorf( + "cannot perform schema change on function %s%s because it is required by the database system", + fnObj.FuncName.Object(), ol.Signature(true), + ), + ) + } + + fnID := funcdesc.UserDefinedFunctionOIDToID(ol.Oid) + b.mustOwn(fnID) + b.ensureDescriptor(fnID) + return b.descCache[fnID].ers +} + func (b *builderState) ensureDescriptor(id catid.DescID) { if _, found := b.descCache[id]; found { return diff --git a/pkg/sql/schemachanger/scbuild/event_log.go b/pkg/sql/schemachanger/scbuild/event_log.go index 6ed6934e95c9..116d3797a8b9 100644 --- a/pkg/sql/schemachanger/scbuild/event_log.go +++ b/pkg/sql/schemachanger/scbuild/event_log.go @@ -343,6 +343,10 @@ func (pb payloadBuilder) build(b buildCtx) logpb.EventPayload { Comment: e.Comment, NullComment: pb.TargetStatus != scpb.Status_PUBLIC, } + case *scpb.Function: + return &eventpb.DropFunction{ + FunctionName: fullyQualifiedName(b, e), + } } if _, _, tbl := scpb.FindTable(b.QueryByID(screl.GetDescID(pb.Element()))); tbl != nil { return &eventpb.AlterTable{ diff --git a/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/BUILD.bazel b/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/BUILD.bazel index 0021dc016c6e..ade7f1c7fdbd 100644 --- a/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/BUILD.bazel +++ b/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/BUILD.bazel @@ -13,6 +13,7 @@ go_library( "create_index.go", "dependencies.go", "drop_database.go", + "drop_function.go", "drop_index.go", "drop_owned_by.go", "drop_schema.go", diff --git a/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_drop_column.go b/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_drop_column.go index 8fdebc503322..a2e73aa1faba 100644 --- a/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_drop_column.go +++ b/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_drop_column.go @@ -261,6 +261,15 @@ func dropColumn( dropRestrictDescriptor(b, e.SequenceID) undroppedSeqBackrefsToCheck.Add(e.SequenceID) } + case *scpb.FunctionBody: + if behavior != tree.DropCascade { + _, _, fnName := scpb.FindFunctionName(b.QueryByID(e.FunctionID)) + panic(sqlerrors.NewDependentObjectErrorf( + "cannot drop column %q because function %q depends on it", + cn.Name, fnName.Name), + ) + } + dropCascadeDescriptor(b, e.FunctionID) default: b.Drop(e) } @@ -362,6 +371,12 @@ func walkDropColumnDependencies(b BuildCtx, col *scpb.Column, fn func(e scpb.Ele catalog.MakeTableColSet(elt.ReferencedColumnIDs...).Contains(col.ColumnID) { fn(e) } + case *scpb.FunctionBody: + for _, ref := range elt.UsesTables { + if ref.TableID == col.TableID && catalog.MakeTableColSet(ref.ColumnIDs...).Contains(col.ColumnID) { + fn(e) + } + } } }) } diff --git a/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/dependencies.go b/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/dependencies.go index aee5948e03c8..8dc30f7837e9 100644 --- a/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/dependencies.go +++ b/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/dependencies.go @@ -310,6 +310,9 @@ type NameResolver interface { // ResolveIndex retrieves an index by name and returns its elements. ResolveIndex(relationID catid.DescID, indexName tree.Name, p ResolveParams) ElementResultSet + // ResolveUDF retrieves a user defined function and returns its elements. + ResolveUDF(fnObj *tree.FuncObj, p ResolveParams) ElementResultSet + // ResolveIndexByName retrieves a table which contains the target // index and returns its elements. Name of database, schema or table may be // missing. diff --git a/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/drop_function.go b/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/drop_function.go new file mode 100644 index 000000000000..b790109d4e2f --- /dev/null +++ b/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/drop_function.go @@ -0,0 +1,38 @@ +// Copyright 2023 The Cockroach Authors. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +package scbuildstmt + +import ( + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scerrors" + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" + "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" +) + +func DropFunction(b BuildCtx, n *tree.DropFunction) { + if n.DropBehavior == tree.DropCascade { + // TODO(chengxiong): remove this when we allow UDF usage. + panic(scerrors.NotImplementedErrorf(n, "cascade dropping functions")) + } + for _, f := range n.Functions { + elts := b.ResolveUDF(&f, ResolveParams{ + IsExistenceOptional: n.IfExists, + }) + _, _, fn := scpb.FindFunction(elts) + if fn == nil { + continue + } + f.FuncName.ObjectNamePrefix = b.NamePrefix(fn) + dropRestrictDescriptor(b, fn.FunctionID) + b.LogEventForExistingTarget(fn) + b.IncrementSubWorkID() + b.IncrementSchemaChangeDropCounter("function") + } +} diff --git a/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/drop_index.go b/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/drop_index.go index b3b2915d28c9..95aeb29531e1 100644 --- a/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/drop_index.go +++ b/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/drop_index.go @@ -129,6 +129,9 @@ func dropSecondaryIndex( // dependents. maybeDropDependentViews(next, sie, indexName.Index.String(), dropBehavior) + // Maybe drop dependent functions. + maybeDropDependentFunctions(next, sie, indexName.Index.String(), dropBehavior) + // Maybe drop dependent FK constraints. // A PK or unique constraint is required to serve an inbound FK constraint. // It is possible that there is an inbound FK constraint 'fk' and it's @@ -186,6 +189,33 @@ func maybeDropDependentViews( }) } +func maybeDropDependentFunctions( + b BuildCtx, + toBeDroppedIndex *scpb.SecondaryIndex, + toBeDroppedIndexName string, + dropBehavior tree.DropBehavior, +) { + scpb.ForEachFunctionBody(b.BackReferences(toBeDroppedIndex.TableID), func( + current scpb.Status, target scpb.TargetStatus, e *scpb.FunctionBody, + ) { + for _, forwardRef := range e.UsesTables { + if forwardRef.IndexID != toBeDroppedIndex.IndexID { + continue + } + // This view depends on the to-be-dropped index; + if dropBehavior != tree.DropCascade { + // Get view name for the error message + _, _, fnName := scpb.FindFunctionName(b.QueryByID(e.FunctionID)) + panic(errors.WithHintf( + sqlerrors.NewDependentObjectErrorf("cannot drop index %q because function %q depends on it", + toBeDroppedIndexName, fnName.Name), "you can drop %q instead.", fnName.Name)) + } else { + dropCascadeDescriptor(b, e.FunctionID) + } + } + }) +} + // maybeDropDependentFKConstraints attempts to drop all FK constraints // that depend on the to be dropped index if CASCADE. // A FK constraint can only exist if there is `PRIMARY KEY` or `UNIQUE` diff --git a/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/helpers.go b/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/helpers.go index a6761ebbee24..2990b8acc6c0 100644 --- a/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/helpers.go +++ b/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/helpers.go @@ -30,6 +30,11 @@ import ( func qualifiedName(b BuildCtx, id catid.DescID) string { _, _, ns := scpb.FindNamespace(b.QueryByID(id)) + if ns == nil { + // Function descriptors don't have namespace. So we need to handle this + // special case here. + return qualifiedFunctionName(b, id) + } _, _, sc := scpb.FindNamespace(b.QueryByID(ns.SchemaID)) _, _, db := scpb.FindNamespace(b.QueryByID(ns.DatabaseID)) if db == nil { @@ -41,6 +46,16 @@ func qualifiedName(b BuildCtx, id catid.DescID) string { return db.Name + "." + sc.Name + "." + ns.Name } +func qualifiedFunctionName(b BuildCtx, id catid.DescID) string { + elts := b.QueryByID(id) + _, _, fnName := scpb.FindFunctionName(elts) + _, _, objParent := scpb.FindObjectParent(elts) + _, _, scName := scpb.FindNamespace(b.QueryByID(objParent.ParentSchemaID)) + _, _, scParent := scpb.FindSchemaParent(b.QueryByID(objParent.ParentSchemaID)) + _, _, dbName := scpb.FindNamespace(b.QueryByID(scParent.ParentDatabaseID)) + return dbName.Name + "." + scName.Name + "." + fnName.Name +} + func simpleName(b BuildCtx, id catid.DescID) string { _, _, ns := scpb.FindNamespace(b.QueryByID(id)) return ns.Name @@ -204,6 +219,8 @@ func dropCascadeDescriptor(b BuildCtx, id catid.DescID) { dropCascadeDescriptor(next, t.TypeID) case *scpb.CompositeType: dropCascadeDescriptor(next, t.TypeID) + case *scpb.FunctionBody: + dropCascadeDescriptor(next, t.FunctionID) case *scpb.Column, *scpb.ColumnType, *scpb.SecondaryIndexPartial: // These only have type references. break diff --git a/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/process.go b/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/process.go index a99164a142a2..1747fd0a7f8d 100644 --- a/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/process.go +++ b/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/process.go @@ -103,6 +103,7 @@ var supportedStatements = map[reflect.Type]supportedStatement{ reflect.TypeOf((*tree.CommentOnIndex)(nil)): {fn: CommentOnIndex, on: true, minSupportedClusterVersion: clusterversion.V22_2Start}, reflect.TypeOf((*tree.CommentOnConstraint)(nil)): {fn: CommentOnConstraint, on: true, minSupportedClusterVersion: clusterversion.V22_2Start}, reflect.TypeOf((*tree.DropIndex)(nil)): {fn: DropIndex, on: true, minSupportedClusterVersion: clusterversion.V23_1Start}, + reflect.TypeOf((*tree.DropFunction)(nil)): {fn: DropFunction, on: true, minSupportedClusterVersion: clusterversion.V23_1Start}, } func init() { diff --git a/pkg/sql/schemachanger/scbuild/testdata/drop_function b/pkg/sql/schemachanger/scbuild/testdata/drop_function new file mode 100644 index 000000000000..ed694de7c4a3 --- /dev/null +++ b/pkg/sql/schemachanger/scbuild/testdata/drop_function @@ -0,0 +1,43 @@ +setup +CREATE TABLE t( + a INT PRIMARY KEY, + b INT, + C INT, + INDEX t_idx_b(b), + INDEX t_idx_c(c) +); +CREATE SEQUENCE sq1; +CREATE VIEW v AS SELECT a FROM t; +CREATE TYPE notmyworkday AS ENUM ('Monday', 'Tuesday'); +CREATE FUNCTION f(a notmyworkday) RETURNS INT IMMUTABLE LANGUAGE SQL AS $$ + SELECT a FROM t; + SELECT b FROM t@t_idx_b; + SELECT c FROM t@t_idx_c; + SELECT a FROM v; + SELECT nextval('sq1'); +$$; +---- + +build +DROP FUNCTION f; +---- +- [[Owner:{DescID: 109}, ABSENT], PUBLIC] + {descriptorId: 109, owner: root} +- [[UserPrivileges:{DescID: 109, Name: admin}, ABSENT], PUBLIC] + {descriptorId: 109, privileges: "2", userName: admin} +- [[UserPrivileges:{DescID: 109, Name: root}, ABSENT], PUBLIC] + {descriptorId: 109, privileges: "2", userName: root} +- [[Function:{DescID: 109}, ABSENT], PUBLIC] + {functionId: 109, params: [{class: {class: IN}, name: a, type: {closedTypeIds: [107, 108], type: {family: EnumFamily, oid: 100107, udtMetadata: {arrayTypeOid: 100108}}}}], returnType: {type: {family: IntFamily, oid: 20, width: 64}}} +- [[ObjectParent:{DescID: 109, ReferencedDescID: 101}, ABSENT], PUBLIC] + {objectId: 109, parentSchemaId: 101} +- [[FunctionName:{DescID: 109}, ABSENT], PUBLIC] + {functionId: 109, name: f} +- [[FunctionVolatility:{DescID: 109}, ABSENT], PUBLIC] + {functionId: 109, volatility: {volatility: IMMUTABLE}} +- [[FunctionLeakProof:{DescID: 109}, ABSENT], PUBLIC] + {functionId: 109} +- [[FunctionNullInputBehavior:{DescID: 109}, ABSENT], PUBLIC] + {functionId: 109, nullInputBehavior: {nullInputBehavior: CALLED_ON_NULL_INPUT}} +- [[FunctionBody:{DescID: 109}, ABSENT], PUBLIC] + {body: "SELECT a FROM defaultdb.public.t;\nSELECT b FROM defaultdb.public.t@t_idx_b;\nSELECT c FROM defaultdb.public.t@t_idx_c;\nSELECT a FROM defaultdb.public.v;\nSELECT nextval(105:::REGCLASS);", functionId: 109, lang: {lang: SQL}, usesSequenceIds: [105], usesTables: [{columnIds: [1], tableId: 104}, {columnIds: [2], indexId: 2, tableId: 104}, {columnIds: [3], indexId: 3, tableId: 104}], usesTypeIds: [107, 108], usesViews: [{columnIds: [1], viewId: 106}]} diff --git a/pkg/sql/schemachanger/scdecomp/testdata/function b/pkg/sql/schemachanger/scdecomp/testdata/function index b83bb4d9d958..30581174ff11 100644 --- a/pkg/sql/schemachanger/scdecomp/testdata/function +++ b/pkg/sql/schemachanger/scdecomp/testdata/function @@ -77,12 +77,12 @@ ElementState: Status: PUBLIC - UserPrivileges: descriptorId: 110 - privileges: 2 + privileges: "2" userName: admin Status: PUBLIC - UserPrivileges: descriptorId: 110 - privileges: 2 + privileges: "2" userName: root Status: PUBLIC - ObjectParent: diff --git a/pkg/sql/schemachanger/scdeps/sctestdeps/BUILD.bazel b/pkg/sql/schemachanger/scdeps/sctestdeps/BUILD.bazel index deb9e209c24c..e21ab526efed 100644 --- a/pkg/sql/schemachanger/scdeps/sctestdeps/BUILD.bazel +++ b/pkg/sql/schemachanger/scdeps/sctestdeps/BUILD.bazel @@ -26,6 +26,7 @@ go_library( "//pkg/sql/catalog/dbdesc", "//pkg/sql/catalog/descbuilder", "//pkg/sql/catalog/descpb", + "//pkg/sql/catalog/funcdesc", "//pkg/sql/catalog/nstree", "//pkg/sql/catalog/schemadesc", "//pkg/sql/catalog/tabledesc", diff --git a/pkg/sql/schemachanger/scdeps/sctestdeps/test_deps.go b/pkg/sql/schemachanger/scdeps/sctestdeps/test_deps.go index 6360fb119556..b6b2053c6b3d 100644 --- a/pkg/sql/schemachanger/scdeps/sctestdeps/test_deps.go +++ b/pkg/sql/schemachanger/scdeps/sctestdeps/test_deps.go @@ -28,6 +28,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descbuilder" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/funcdesc" "github.com/cockroachdb/cockroach/pkg/sql/catalog/nstree" "github.com/cockroachdb/cockroach/pkg/sql/catalog/schemadesc" "github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc" @@ -1180,15 +1181,27 @@ func (s *TestState) AddTableForStatsRefresh(id descpb.ID) { func (s *TestState) ResolveFunction( ctx context.Context, name *tree.UnresolvedName, path tree.SearchPath, ) (*tree.ResolvedFunctionDefinition, error) { - // TODO(chengxiong): add UDF support for test. - fn, err := name.ToFunctionName() + fnName, err := name.ToFunctionName() if err != nil { return nil, err } - fd, err := tree.GetBuiltinFuncDefinitionOrFail(fn, path) + fd, err := tree.GetBuiltinFuncDefinition(fnName, path) if err != nil { return nil, err } + if fd != nil { + return fd, nil + } + + _, sc := s.mayResolvePrefix(fnName.ObjectNamePrefix) + scDesc, err := catalog.AsSchemaDescriptor(sc) + if err != nil { + return nil, err + } + fd, found := scDesc.GetResolvedFuncDefinition(fnName.Object()) + if !found { + return nil, errors.Newf("function %s not found", fnName.String()) + } return fd, nil } @@ -1196,18 +1209,34 @@ func (s *TestState) ResolveFunction( func (s *TestState) ResolveFunctionByOID( ctx context.Context, oid oid.Oid, ) (string, *tree.Overload, error) { - // TODO(chengxiong): add UDF support for test. - name, ok := tree.OidToBuiltinName[oid] - if !ok { + if !funcdesc.IsOIDUserDefinedFunc(oid) { + name, ok := tree.OidToBuiltinName[oid] + if !ok { + return "", nil, errors.Newf("function %d not found", oid) + } + funcDef := tree.FunDefs[name] + for _, o := range funcDef.Definition { + if o.Oid == oid { + return funcDef.Name, o, nil + } + } return "", nil, errors.Newf("function %d not found", oid) } - funcDef := tree.FunDefs[name] - for _, o := range funcDef.Definition { - if o.Oid == oid { - return funcDef.Name, o, nil - } + + fnID := funcdesc.UserDefinedFunctionOIDToID(oid) + desc, err := s.mustReadImmutableDescriptor(fnID) + if err != nil { + return "", nil, err + } + fnDesc, err := catalog.AsFunctionDescriptor(desc) + if err != nil { + return "", nil, err + } + ol, err := fnDesc.ToOverload() + if err != nil { + return "", nil, err } - return "", nil, errors.Newf("function %d not found", oid) + return fnDesc.GetName(), ol, nil } // ZoneConfigGetter implements scexec.Dependencies. diff --git a/pkg/sql/schemachanger/scexec/scmutationexec/references.go b/pkg/sql/schemachanger/scexec/scmutationexec/references.go index 20ef3048a585..d2a73949134d 100644 --- a/pkg/sql/schemachanger/scexec/scmutationexec/references.go +++ b/pkg/sql/schemachanger/scexec/scmutationexec/references.go @@ -294,11 +294,11 @@ func updateBackReferencesInSequences( return nil } -func (m *immediateVisitor) RemoveBackReferencesInRelations( +func (i *immediateVisitor) RemoveBackReferencesInRelations( ctx context.Context, op scop.RemoveBackReferencesInRelations, ) error { for _, relationID := range op.RelationIDs { - if err := removeViewBackReferencesInRelation(ctx, m, relationID, op.BackReferencedID); err != nil { + if err := removeViewBackReferencesInRelation(ctx, i, relationID, op.BackReferencedID); err != nil { return err } } @@ -322,3 +322,21 @@ func removeViewBackReferencesInRelation( tbl.DependedOnBy = newBackRefs return nil } + +func (i *immediateVisitor) RemoveObjectParent( + ctx context.Context, op scop.RemoveObjectParent, +) error { + obj, err := i.checkOutDescriptor(ctx, op.ObjectID) + if err != nil { + return err + } + switch obj.DescriptorType() { + case catalog.Function: + sc, err := i.checkOutSchema(ctx, op.ParentSchemaID) + if err != nil { + return err + } + sc.RemoveFunction(obj.GetName(), obj.GetID()) + } + return nil +} diff --git a/pkg/sql/schemachanger/scop/immediate_mutation.go b/pkg/sql/schemachanger/scop/immediate_mutation.go index 5106e9d01156..e8d546cad3a8 100644 --- a/pkg/sql/schemachanger/scop/immediate_mutation.go +++ b/pkg/sql/schemachanger/scop/immediate_mutation.go @@ -625,3 +625,9 @@ type RemoveColumnFromIndex struct { Ordinal uint32 InvertedKind catpb.InvertedIndexColumnKind } + +type RemoveObjectParent struct { + immediateMutationOp + ObjectID descpb.ID + ParentSchemaID descpb.ID +} diff --git a/pkg/sql/schemachanger/scop/immediate_mutation_visitor_generated.go b/pkg/sql/schemachanger/scop/immediate_mutation_visitor_generated.go index 9a373efd7d63..4b0cd4af7748 100644 --- a/pkg/sql/schemachanger/scop/immediate_mutation_visitor_generated.go +++ b/pkg/sql/schemachanger/scop/immediate_mutation_visitor_generated.go @@ -97,6 +97,7 @@ type ImmediateMutationVisitor interface { RemoveConstraintComment(context.Context, RemoveConstraintComment) error AddColumnToIndex(context.Context, AddColumnToIndex) error RemoveColumnFromIndex(context.Context, RemoveColumnFromIndex) error + RemoveObjectParent(context.Context, RemoveObjectParent) error } // Visit is part of the ImmediateMutationOp interface. @@ -473,3 +474,8 @@ func (op AddColumnToIndex) Visit(ctx context.Context, v ImmediateMutationVisitor func (op RemoveColumnFromIndex) Visit(ctx context.Context, v ImmediateMutationVisitor) error { return v.RemoveColumnFromIndex(ctx, op) } + +// Visit is part of the ImmediateMutationOp interface. +func (op RemoveObjectParent) Visit(ctx context.Context, v ImmediateMutationVisitor) error { + return v.RemoveObjectParent(ctx, op) +} diff --git a/pkg/sql/schemachanger/scplan/internal/opgen/BUILD.bazel b/pkg/sql/schemachanger/scplan/internal/opgen/BUILD.bazel index ea41b8443b8a..f6cc7f362a8a 100644 --- a/pkg/sql/schemachanger/scplan/internal/opgen/BUILD.bazel +++ b/pkg/sql/schemachanger/scplan/internal/opgen/BUILD.bazel @@ -28,6 +28,13 @@ go_library( "opgen_enum_type.go", "opgen_enum_type_value.go", "opgen_foreign_key_constraint.go", + "opgen_function.go", + "opgen_function_body.go", + "opgen_function_leakproof.go", + "opgen_function_name.go", + "opgen_function_null_input.go", + "opgen_function_param_default.go", + "opgen_function_volatility.go", "opgen_index_column.go", "opgen_index_comment.go", "opgen_index_data.go", diff --git a/pkg/sql/schemachanger/scplan/internal/opgen/opgen_function.go b/pkg/sql/schemachanger/scplan/internal/opgen/opgen_function.go new file mode 100644 index 000000000000..19e012198d46 --- /dev/null +++ b/pkg/sql/schemachanger/scplan/internal/opgen/opgen_function.go @@ -0,0 +1,48 @@ +// Copyright 2023 The Cockroach Authors. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +package opgen + +import ( + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scop" + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" +) + +func init() { + opRegistry.register((*scpb.Function)(nil), + toPublic( + scpb.Status_ABSENT, + equiv(scpb.Status_DROPPED), + to(scpb.Status_PUBLIC, + emit(func(this *scpb.Function) *scop.NotImplemented { + return notImplemented(this) + }), + ), + ), + toAbsent( + scpb.Status_PUBLIC, + to(scpb.Status_DROPPED, + revertible(false), + emit(func(this *scpb.Function) *scop.MarkDescriptorAsDropped { + return &scop.MarkDescriptorAsDropped{ + DescriptorID: this.FunctionID, + } + }), + ), + to(scpb.Status_ABSENT, + emit(func(this *scpb.Function) *scop.DeleteDescriptor { + return &scop.DeleteDescriptor{ + DescriptorID: this.FunctionID, + } + }), + ), + ), + ) +} diff --git a/pkg/sql/schemachanger/scplan/internal/opgen/opgen_function_body.go b/pkg/sql/schemachanger/scplan/internal/opgen/opgen_function_body.go new file mode 100644 index 000000000000..52a726782773 --- /dev/null +++ b/pkg/sql/schemachanger/scplan/internal/opgen/opgen_function_body.go @@ -0,0 +1,60 @@ +// Copyright 2023 The Cockroach Authors. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +package opgen + +import ( + "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scop" + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" +) + +func init() { + opRegistry.register((*scpb.FunctionBody)(nil), + toPublic( + scpb.Status_ABSENT, + to(scpb.Status_PUBLIC, + emit(func(this *scpb.FunctionBody) *scop.NotImplemented { + return notImplemented(this) + }), + ), + ), + toAbsent( + scpb.Status_PUBLIC, + to(scpb.Status_ABSENT, + emit(func(this *scpb.FunctionBody) *scop.RemoveBackReferenceInTypes { + if len(this.UsesTypeIDs) == 0 { + return nil + } + return &scop.RemoveBackReferenceInTypes{ + BackReferencedDescriptorID: this.FunctionID, + TypeIDs: this.UsesTypeIDs, + } + }), + emit(func(this *scpb.FunctionBody) *scop.RemoveBackReferencesInRelations { + var relationIDs []descpb.ID + for _, ref := range this.UsesTables { + relationIDs = append(relationIDs, ref.TableID) + } + for _, ref := range this.UsesViews { + relationIDs = append(relationIDs, ref.ViewID) + } + relationIDs = append(relationIDs, this.UsesSequenceIDs...) + if len(relationIDs) == 0 { + return nil + } + return &scop.RemoveBackReferencesInRelations{ + BackReferencedID: this.FunctionID, + RelationIDs: relationIDs, + } + })), + ), + ) +} diff --git a/pkg/sql/schemachanger/scplan/internal/opgen/opgen_function_leakproof.go b/pkg/sql/schemachanger/scplan/internal/opgen/opgen_function_leakproof.go new file mode 100644 index 000000000000..5a7e4711c39c --- /dev/null +++ b/pkg/sql/schemachanger/scplan/internal/opgen/opgen_function_leakproof.go @@ -0,0 +1,37 @@ +// Copyright 2023 The Cockroach Authors. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +package opgen + +import ( + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scop" + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" +) + +func init() { + opRegistry.register((*scpb.FunctionLeakProof)(nil), + toPublic( + scpb.Status_ABSENT, + to(scpb.Status_PUBLIC, + emit(func(this *scpb.FunctionLeakProof) *scop.NotImplemented { + return notImplemented(this) + }), + ), + ), + toAbsent( + scpb.Status_PUBLIC, + to(scpb.Status_ABSENT, + emit(func(this *scpb.FunctionLeakProof) *scop.NotImplemented { + return notImplemented(this) + }), + ), + ), + ) +} diff --git a/pkg/sql/schemachanger/scplan/internal/opgen/opgen_function_name.go b/pkg/sql/schemachanger/scplan/internal/opgen/opgen_function_name.go new file mode 100644 index 000000000000..d392cd954886 --- /dev/null +++ b/pkg/sql/schemachanger/scplan/internal/opgen/opgen_function_name.go @@ -0,0 +1,37 @@ +// Copyright 2023 The Cockroach Authors. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +package opgen + +import ( + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scop" + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" +) + +func init() { + opRegistry.register((*scpb.FunctionName)(nil), + toPublic( + scpb.Status_ABSENT, + to(scpb.Status_PUBLIC, + emit(func(this *scpb.FunctionName) *scop.NotImplemented { + return notImplemented(this) + }), + ), + ), + toAbsent( + scpb.Status_PUBLIC, + to(scpb.Status_ABSENT, + emit(func(this *scpb.FunctionName) *scop.NotImplemented { + return notImplemented(this) + }), + ), + ), + ) +} diff --git a/pkg/sql/schemachanger/scplan/internal/opgen/opgen_function_null_input.go b/pkg/sql/schemachanger/scplan/internal/opgen/opgen_function_null_input.go new file mode 100644 index 000000000000..a5787b684c62 --- /dev/null +++ b/pkg/sql/schemachanger/scplan/internal/opgen/opgen_function_null_input.go @@ -0,0 +1,37 @@ +// Copyright 2023 The Cockroach Authors. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +package opgen + +import ( + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scop" + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" +) + +func init() { + opRegistry.register((*scpb.FunctionNullInputBehavior)(nil), + toPublic( + scpb.Status_ABSENT, + to(scpb.Status_PUBLIC, + emit(func(this *scpb.FunctionNullInputBehavior) *scop.NotImplemented { + return notImplemented(this) + }), + ), + ), + toAbsent( + scpb.Status_PUBLIC, + to(scpb.Status_ABSENT, + emit(func(this *scpb.FunctionNullInputBehavior) *scop.NotImplemented { + return notImplemented(this) + }), + ), + ), + ) +} diff --git a/pkg/sql/schemachanger/scplan/internal/opgen/opgen_function_param_default.go b/pkg/sql/schemachanger/scplan/internal/opgen/opgen_function_param_default.go new file mode 100644 index 000000000000..bb467a5e33d7 --- /dev/null +++ b/pkg/sql/schemachanger/scplan/internal/opgen/opgen_function_param_default.go @@ -0,0 +1,39 @@ +// Copyright 2023 The Cockroach Authors. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +package opgen + +import ( + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scop" + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" +) + +func init() { + opRegistry.register((*scpb.FunctionParamDefaultExpression)(nil), + toPublic( + scpb.Status_ABSENT, + to(scpb.Status_PUBLIC, + // TODO(chengxiong): add operations when default value is supported. + emit(func(this *scpb.FunctionParamDefaultExpression) *scop.NotImplemented { + return notImplemented(this) + }), + ), + ), + toAbsent( + scpb.Status_PUBLIC, + to(scpb.Status_ABSENT, + // TODO(chengxiong): add operations when default value is supported. + emit(func(this *scpb.FunctionParamDefaultExpression) *scop.NotImplemented { + return notImplemented(this) + }), + ), + ), + ) +} diff --git a/pkg/sql/schemachanger/scplan/internal/opgen/opgen_function_volatility.go b/pkg/sql/schemachanger/scplan/internal/opgen/opgen_function_volatility.go new file mode 100644 index 000000000000..f6ed74e89d08 --- /dev/null +++ b/pkg/sql/schemachanger/scplan/internal/opgen/opgen_function_volatility.go @@ -0,0 +1,37 @@ +// Copyright 2023 The Cockroach Authors. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +package opgen + +import ( + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scop" + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" +) + +func init() { + opRegistry.register((*scpb.FunctionVolatility)(nil), + toPublic( + scpb.Status_ABSENT, + to(scpb.Status_PUBLIC, + emit(func(this *scpb.FunctionVolatility) *scop.NotImplemented { + return notImplemented(this) + }), + ), + ), + toAbsent( + scpb.Status_PUBLIC, + to(scpb.Status_ABSENT, + emit(func(this *scpb.FunctionVolatility) *scop.NotImplemented { + return notImplemented(this) + }), + ), + ), + ) +} diff --git a/pkg/sql/schemachanger/scplan/internal/opgen/opgen_object_parent.go b/pkg/sql/schemachanger/scplan/internal/opgen/opgen_object_parent.go index 781b54d6fc7c..755135e44bd5 100644 --- a/pkg/sql/schemachanger/scplan/internal/opgen/opgen_object_parent.go +++ b/pkg/sql/schemachanger/scplan/internal/opgen/opgen_object_parent.go @@ -30,6 +30,12 @@ func init() { to(scpb.Status_ABSENT, // TODO(postamar): remove revertibility constraint when possible revertible(false), + emit(func(this *scpb.ObjectParent) *scop.RemoveObjectParent { + return &scop.RemoveObjectParent{ + ObjectID: this.ObjectID, + ParentSchemaID: this.ParentSchemaID, + } + }), ), ), ) diff --git a/pkg/sql/schemachanger/scplan/internal/rules/current/helpers.go b/pkg/sql/schemachanger/scplan/internal/rules/current/helpers.go index 6700a6987fb8..ee22ece9a7f8 100644 --- a/pkg/sql/schemachanger/scplan/internal/rules/current/helpers.go +++ b/pkg/sql/schemachanger/scplan/internal/rules/current/helpers.go @@ -49,7 +49,7 @@ var descriptorIsNotBeingDropped = screl.Schema.DefNotJoin1( func isDescriptor(e scpb.Element) bool { switch e.(type) { case *scpb.Database, *scpb.Schema, *scpb.Table, *scpb.View, *scpb.Sequence, - *scpb.AliasType, *scpb.EnumType, *scpb.CompositeType: + *scpb.AliasType, *scpb.EnumType, *scpb.CompositeType, *scpb.Function: return true } return false @@ -133,6 +133,11 @@ func getExpression(element scpb.Element) (*scpb.Expression, error) { return nil, nil } return &e.Expression, nil + case *scpb.FunctionParamDefaultExpression: + if e == nil { + return nil, nil + } + return &e.Expression, nil } return nil, errors.AssertionFailedf("element %T does not have an embedded scpb.Expression", element) } diff --git a/pkg/sql/schemachanger/scplan/internal/rules/current/op_drop.go b/pkg/sql/schemachanger/scplan/internal/rules/current/op_drop.go index b8a801347cd4..18a1fdb40031 100644 --- a/pkg/sql/schemachanger/scplan/internal/rules/current/op_drop.go +++ b/pkg/sql/schemachanger/scplan/internal/rules/current/op_drop.go @@ -241,6 +241,10 @@ func init() { (*scpb.UserPrivileges)(nil), (*scpb.EnumTypeValue)(nil), (*scpb.TablePartitioning)(nil), + (*scpb.FunctionName)(nil), + (*scpb.FunctionVolatility)(nil), + (*scpb.FunctionLeakProof)(nil), + (*scpb.FunctionNullInputBehavior)(nil), ), JoinOnDescID(desc, dep, descID), diff --git a/pkg/sql/schemachanger/scplan/internal/rules/current/testdata/deprules b/pkg/sql/schemachanger/scplan/internal/rules/current/testdata/deprules index b442398cb6cb..e3dd64a83d15 100644 --- a/pkg/sql/schemachanger/scplan/internal/rules/current/testdata/deprules +++ b/pkg/sql/schemachanger/scplan/internal/rules/current/testdata/deprules @@ -14,7 +14,7 @@ ToPublicOrTransient($target1, $target2): - $target2[TargetStatus] IN [PUBLIC, TRANSIENT_ABSENT] descriptorIsNotBeingDropped-23.1($element): not-join: - - $descriptor[Type] IN ['*scpb.Database', '*scpb.Schema', '*scpb.View', '*scpb.Sequence', '*scpb.Table', '*scpb.EnumType', '*scpb.AliasType', '*scpb.CompositeType'] + - $descriptor[Type] IN ['*scpb.Database', '*scpb.Schema', '*scpb.View', '*scpb.Sequence', '*scpb.Table', '*scpb.EnumType', '*scpb.AliasType', '*scpb.CompositeType', '*scpb.Function'] - joinTarget($descriptor, $descriptor-Target) - joinOnDescID($descriptor, $element, $id) - $descriptor-Target[TargetStatus] = ABSENT @@ -2036,8 +2036,8 @@ deprules kind: SameStagePrecedence to: referencing-via-attr-Node query: - - $referenced-descriptor[Type] IN ['*scpb.Database', '*scpb.Schema', '*scpb.View', '*scpb.Sequence', '*scpb.Table', '*scpb.EnumType', '*scpb.AliasType', '*scpb.CompositeType'] - - $referencing-via-attr[Type] IN ['*scpb.ColumnFamily', '*scpb.TableComment', '*scpb.RowLevelTTL', '*scpb.TableZoneConfig', '*scpb.TablePartitioning', '*scpb.TableLocalityGlobal', '*scpb.TableLocalityPrimaryRegion', '*scpb.TableLocalitySecondaryRegion', '*scpb.TableLocalityRegionalByRow', '*scpb.ColumnName', '*scpb.ColumnType', '*scpb.ColumnDefaultExpression', '*scpb.ColumnOnUpdateExpression', '*scpb.SequenceOwner', '*scpb.ColumnComment', '*scpb.IndexName', '*scpb.IndexPartitioning', '*scpb.SecondaryIndexPartial', '*scpb.IndexComment', '*scpb.IndexColumn', '*scpb.ConstraintWithoutIndexName', '*scpb.ConstraintComment', '*scpb.Namespace', '*scpb.Owner', '*scpb.UserPrivileges', '*scpb.DatabaseRegionConfig', '*scpb.DatabaseRoleSetting', '*scpb.DatabaseComment', '*scpb.SchemaParent', '*scpb.SchemaComment', '*scpb.ObjectParent', '*scpb.EnumTypeValue', '*scpb.CompositeTypeAttrType', '*scpb.CompositeTypeAttrName'] + - $referenced-descriptor[Type] IN ['*scpb.Database', '*scpb.Schema', '*scpb.View', '*scpb.Sequence', '*scpb.Table', '*scpb.EnumType', '*scpb.AliasType', '*scpb.CompositeType', '*scpb.Function'] + - $referencing-via-attr[Type] IN ['*scpb.ColumnFamily', '*scpb.TableComment', '*scpb.RowLevelTTL', '*scpb.TableZoneConfig', '*scpb.TablePartitioning', '*scpb.TableLocalityGlobal', '*scpb.TableLocalityPrimaryRegion', '*scpb.TableLocalitySecondaryRegion', '*scpb.TableLocalityRegionalByRow', '*scpb.ColumnName', '*scpb.ColumnType', '*scpb.ColumnDefaultExpression', '*scpb.ColumnOnUpdateExpression', '*scpb.SequenceOwner', '*scpb.ColumnComment', '*scpb.IndexName', '*scpb.IndexPartitioning', '*scpb.SecondaryIndexPartial', '*scpb.IndexComment', '*scpb.IndexColumn', '*scpb.ConstraintWithoutIndexName', '*scpb.ConstraintComment', '*scpb.Namespace', '*scpb.Owner', '*scpb.UserPrivileges', '*scpb.DatabaseRegionConfig', '*scpb.DatabaseRoleSetting', '*scpb.DatabaseComment', '*scpb.SchemaParent', '*scpb.SchemaComment', '*scpb.ObjectParent', '*scpb.EnumTypeValue', '*scpb.CompositeTypeAttrType', '*scpb.CompositeTypeAttrName', '*scpb.FunctionName', '*scpb.FunctionVolatility', '*scpb.FunctionLeakProof', '*scpb.FunctionNullInputBehavior', '*scpb.FunctionBody', '*scpb.FunctionParamDefaultExpression'] - joinReferencedDescID($referencing-via-attr, $referenced-descriptor, $desc-id) - toAbsent($referenced-descriptor-Target, $referencing-via-attr-Target) - $referenced-descriptor-Node[CurrentStatus] = DROPPED @@ -2052,7 +2052,7 @@ deprules - $referenced-descriptor[Type] = '*scpb.Sequence' - $referenced-descriptor[DescID] = $seqID - $referencing-via-expr[ReferencedSequenceIDs] CONTAINS $seqID - - $referencing-via-expr[Type] IN ['*scpb.ColumnType', '*scpb.ColumnDefaultExpression', '*scpb.ColumnOnUpdateExpression', '*scpb.SecondaryIndexPartial'] + - $referencing-via-expr[Type] IN ['*scpb.ColumnType', '*scpb.ColumnDefaultExpression', '*scpb.ColumnOnUpdateExpression', '*scpb.SecondaryIndexPartial', '*scpb.FunctionParamDefaultExpression'] - toAbsent($referenced-descriptor-Target, $referencing-via-expr-Target) - $referenced-descriptor-Node[CurrentStatus] = DROPPED - $referencing-via-expr-Node[CurrentStatus] = ABSENT @@ -2066,7 +2066,7 @@ deprules - $referenced-descriptor[Type] IN ['*scpb.EnumType', '*scpb.AliasType', '*scpb.CompositeType'] - $referenced-descriptor[DescID] = $fromDescID - $referencing-via-type[ReferencedTypeIDs] CONTAINS $fromDescID - - $referencing-via-type[Type] IN ['*scpb.ColumnType', '*scpb.ColumnDefaultExpression', '*scpb.ColumnOnUpdateExpression', '*scpb.SecondaryIndexPartial'] + - $referencing-via-type[Type] IN ['*scpb.ColumnType', '*scpb.ColumnDefaultExpression', '*scpb.ColumnOnUpdateExpression', '*scpb.SecondaryIndexPartial', '*scpb.FunctionParamDefaultExpression'] - toAbsent($referenced-descriptor-Target, $referencing-via-type-Target) - $referenced-descriptor-Node[CurrentStatus] = DROPPED - $referencing-via-type-Node[CurrentStatus] = ABSENT @@ -2077,8 +2077,8 @@ deprules kind: Precedence to: dependent-Node query: - - $descriptor[Type] IN ['*scpb.Database', '*scpb.Schema', '*scpb.View', '*scpb.Sequence', '*scpb.Table', '*scpb.EnumType', '*scpb.AliasType', '*scpb.CompositeType'] - - $dependent[Type] IN ['*scpb.ColumnFamily', '*scpb.TableComment', '*scpb.RowLevelTTL', '*scpb.TableZoneConfig', '*scpb.TablePartitioning', '*scpb.TableLocalityGlobal', '*scpb.TableLocalityPrimaryRegion', '*scpb.TableLocalitySecondaryRegion', '*scpb.TableLocalityRegionalByRow', '*scpb.ColumnName', '*scpb.ColumnType', '*scpb.ColumnDefaultExpression', '*scpb.ColumnOnUpdateExpression', '*scpb.SequenceOwner', '*scpb.ColumnComment', '*scpb.IndexName', '*scpb.IndexPartitioning', '*scpb.SecondaryIndexPartial', '*scpb.IndexComment', '*scpb.IndexColumn', '*scpb.ConstraintWithoutIndexName', '*scpb.ConstraintComment', '*scpb.Namespace', '*scpb.Owner', '*scpb.UserPrivileges', '*scpb.DatabaseRegionConfig', '*scpb.DatabaseRoleSetting', '*scpb.DatabaseComment', '*scpb.SchemaParent', '*scpb.SchemaComment', '*scpb.ObjectParent', '*scpb.EnumTypeValue', '*scpb.CompositeTypeAttrType', '*scpb.CompositeTypeAttrName'] + - $descriptor[Type] IN ['*scpb.Database', '*scpb.Schema', '*scpb.View', '*scpb.Sequence', '*scpb.Table', '*scpb.EnumType', '*scpb.AliasType', '*scpb.CompositeType', '*scpb.Function'] + - $dependent[Type] IN ['*scpb.ColumnFamily', '*scpb.TableComment', '*scpb.RowLevelTTL', '*scpb.TableZoneConfig', '*scpb.TablePartitioning', '*scpb.TableLocalityGlobal', '*scpb.TableLocalityPrimaryRegion', '*scpb.TableLocalitySecondaryRegion', '*scpb.TableLocalityRegionalByRow', '*scpb.ColumnName', '*scpb.ColumnType', '*scpb.ColumnDefaultExpression', '*scpb.ColumnOnUpdateExpression', '*scpb.SequenceOwner', '*scpb.ColumnComment', '*scpb.IndexName', '*scpb.IndexPartitioning', '*scpb.SecondaryIndexPartial', '*scpb.IndexComment', '*scpb.IndexColumn', '*scpb.ConstraintWithoutIndexName', '*scpb.ConstraintComment', '*scpb.Namespace', '*scpb.Owner', '*scpb.UserPrivileges', '*scpb.DatabaseRegionConfig', '*scpb.DatabaseRoleSetting', '*scpb.DatabaseComment', '*scpb.SchemaParent', '*scpb.SchemaComment', '*scpb.ObjectParent', '*scpb.EnumTypeValue', '*scpb.CompositeTypeAttrType', '*scpb.CompositeTypeAttrName', '*scpb.FunctionName', '*scpb.FunctionVolatility', '*scpb.FunctionLeakProof', '*scpb.FunctionNullInputBehavior', '*scpb.FunctionBody', '*scpb.FunctionParamDefaultExpression'] - joinOnDescID($descriptor, $dependent, $desc-id) - toAbsent($descriptor-Target, $dependent-Target) - $descriptor-Node[CurrentStatus] = DROPPED @@ -2090,7 +2090,7 @@ deprules kind: PreviousStagePrecedence to: absent-Node query: - - $dropped[Type] IN ['*scpb.Database', '*scpb.Schema', '*scpb.View', '*scpb.Sequence', '*scpb.Table', '*scpb.EnumType', '*scpb.AliasType', '*scpb.CompositeType'] + - $dropped[Type] IN ['*scpb.Database', '*scpb.Schema', '*scpb.View', '*scpb.Sequence', '*scpb.Table', '*scpb.EnumType', '*scpb.AliasType', '*scpb.CompositeType', '*scpb.Function'] - $dropped[DescID] = $_ - $dropped[Self] = $absent - toAbsent($dropped-Target, $absent-Target) @@ -2103,7 +2103,7 @@ deprules kind: SameStagePrecedence to: data-Node query: - - $database[Type] IN ['*scpb.Database', '*scpb.Schema', '*scpb.View', '*scpb.Sequence', '*scpb.Table', '*scpb.EnumType', '*scpb.AliasType', '*scpb.CompositeType'] + - $database[Type] IN ['*scpb.Database', '*scpb.Schema', '*scpb.View', '*scpb.Sequence', '*scpb.Table', '*scpb.EnumType', '*scpb.AliasType', '*scpb.CompositeType', '*scpb.Function'] - $data[Type] = '*scpb.DatabaseData' - joinOnDescID($database, $data, $db-id) - toAbsent($database-Target, $data-Target) @@ -2853,7 +2853,7 @@ deprules kind: SameStagePrecedence to: data-Node query: - - $table[Type] IN ['*scpb.Database', '*scpb.Schema', '*scpb.View', '*scpb.Sequence', '*scpb.Table', '*scpb.EnumType', '*scpb.AliasType', '*scpb.CompositeType'] + - $table[Type] IN ['*scpb.Database', '*scpb.Schema', '*scpb.View', '*scpb.Sequence', '*scpb.Table', '*scpb.EnumType', '*scpb.AliasType', '*scpb.CompositeType', '*scpb.Function'] - $data[Type] = '*scpb.TableData' - joinOnDescID($table, $data, $table-id) - toAbsent($table-Target, $data-Target) diff --git a/pkg/sql/schemachanger/scplan/internal/rules/current/testdata/oprules b/pkg/sql/schemachanger/scplan/internal/rules/current/testdata/oprules index 622bd5bcec4e..f56b1fd58e02 100644 --- a/pkg/sql/schemachanger/scplan/internal/rules/current/testdata/oprules +++ b/pkg/sql/schemachanger/scplan/internal/rules/current/testdata/oprules @@ -14,7 +14,7 @@ ToPublicOrTransient($target1, $target2): - $target2[TargetStatus] IN [PUBLIC, TRANSIENT_ABSENT] descriptorIsNotBeingDropped-23.1($element): not-join: - - $descriptor[Type] IN ['*scpb.Database', '*scpb.Schema', '*scpb.View', '*scpb.Sequence', '*scpb.Table', '*scpb.EnumType', '*scpb.AliasType', '*scpb.CompositeType'] + - $descriptor[Type] IN ['*scpb.Database', '*scpb.Schema', '*scpb.View', '*scpb.Sequence', '*scpb.Table', '*scpb.EnumType', '*scpb.AliasType', '*scpb.CompositeType', '*scpb.Function'] - joinTarget($descriptor, $descriptor-Target) - joinOnDescID($descriptor, $element, $id) - $descriptor-Target[TargetStatus] = ABSENT @@ -153,8 +153,8 @@ oprules - name: skip element removal ops on descriptor drop from: dep-Node query: - - $desc[Type] IN ['*scpb.Database', '*scpb.Schema', '*scpb.View', '*scpb.Sequence', '*scpb.Table', '*scpb.EnumType', '*scpb.AliasType', '*scpb.CompositeType'] - - $dep[Type] IN ['*scpb.ColumnFamily', '*scpb.Owner', '*scpb.UserPrivileges', '*scpb.EnumTypeValue', '*scpb.TablePartitioning'] + - $desc[Type] IN ['*scpb.Database', '*scpb.Schema', '*scpb.View', '*scpb.Sequence', '*scpb.Table', '*scpb.EnumType', '*scpb.AliasType', '*scpb.CompositeType', '*scpb.Function'] + - $dep[Type] IN ['*scpb.ColumnFamily', '*scpb.Owner', '*scpb.UserPrivileges', '*scpb.EnumTypeValue', '*scpb.TablePartitioning', '*scpb.FunctionName', '*scpb.FunctionVolatility', '*scpb.FunctionLeakProof', '*scpb.FunctionNullInputBehavior'] - joinOnDescID($desc, $dep, $desc-id) - joinTarget($desc, $desc-Target) - $desc-Target[TargetStatus] = ABSENT diff --git a/pkg/sql/schemachanger/scplan/testdata/alter_table_drop_column b/pkg/sql/schemachanger/scplan/testdata/alter_table_drop_column index 3f5b181cb46d..c17f0688a043 100644 --- a/pkg/sql/schemachanger/scplan/testdata/alter_table_drop_column +++ b/pkg/sql/schemachanger/scplan/testdata/alter_table_drop_column @@ -14,7 +14,7 @@ SET sql_safe_updates = false; ops ALTER TABLE defaultdb.foo DROP COLUMN v1 CASCADE; ---- -StatementPhase stage 1 of 1 with 15 MutationType ops +StatementPhase stage 1 of 1 with 16 MutationType ops transitions: [[Column:{DescID: 107, ColumnID: 2}, ABSENT], PUBLIC] -> WRITE_ONLY [[ColumnName:{DescID: 107, Name: v1, ColumnID: 2}, ABSENT], PUBLIC] -> ABSENT @@ -67,10 +67,13 @@ StatementPhase stage 1 of 1 with 15 MutationType ops BackReferencedDescriptorID: 108 TypeIDs: - 104 - *scop.RemoveViewBackReferencesInRelations - BackReferencedViewID: 108 + *scop.RemoveBackReferencesInRelations + BackReferencedID: 108 RelationIDs: - 107 + *scop.RemoveObjectParent + ObjectID: 108 + ParentSchemaID: 101 *scop.RemoveDroppedColumnType ColumnID: 2 TableID: 108 @@ -331,7 +334,7 @@ PostCommitPhase stage 7 of 7 with 1 ValidationType op *scop.ValidateIndex IndexID: 3 TableID: 107 -PostCommitNonRevertiblePhase stage 1 of 3 with 24 MutationType ops +PostCommitNonRevertiblePhase stage 1 of 3 with 25 MutationType ops transitions: [[Column:{DescID: 107, ColumnID: 2}, ABSENT], WRITE_ONLY] -> DELETE_ONLY [[IndexColumn:{DescID: 107, ColumnID: 1, IndexID: 1}, ABSENT], PUBLIC] -> ABSENT @@ -391,6 +394,9 @@ PostCommitNonRevertiblePhase stage 1 of 3 with 24 MutationType ops BackReferencedID: 108 RelationIDs: - 107 + *scop.RemoveObjectParent + ObjectID: 108 + ParentSchemaID: 101 *scop.RemoveDroppedColumnType ColumnID: 2 TableID: 108 @@ -991,7 +997,7 @@ ALTER TABLE defaultdb.foo DROP COLUMN v1 CASCADE; ops ALTER TABLE defaultdb.foo DROP COLUMN v2 CASCADE; ---- -StatementPhase stage 1 of 1 with 15 MutationType ops +StatementPhase stage 1 of 1 with 16 MutationType ops transitions: [[Column:{DescID: 107, ColumnID: 3}, ABSENT], PUBLIC] -> WRITE_ONLY [[ColumnName:{DescID: 107, Name: v2, ColumnID: 3}, ABSENT], PUBLIC] -> ABSENT @@ -1037,10 +1043,13 @@ StatementPhase stage 1 of 1 with 15 MutationType ops BackReferencedDescriptorID: 108 TypeIDs: - 104 - *scop.RemoveViewBackReferencesInRelations - BackReferencedViewID: 108 + *scop.RemoveBackReferencesInRelations + BackReferencedID: 108 RelationIDs: - 107 + *scop.RemoveObjectParent + ObjectID: 108 + ParentSchemaID: 101 *scop.RemoveDroppedColumnType ColumnID: 2 TableID: 108 @@ -1320,7 +1329,7 @@ PostCommitPhase stage 7 of 7 with 1 ValidationType op *scop.ValidateIndex IndexID: 3 TableID: 107 -PostCommitNonRevertiblePhase stage 1 of 3 with 25 MutationType ops +PostCommitNonRevertiblePhase stage 1 of 3 with 26 MutationType ops transitions: [[Column:{DescID: 107, ColumnID: 3}, ABSENT], WRITE_ONLY] -> DELETE_ONLY [[IndexColumn:{DescID: 107, ColumnID: 1, IndexID: 1}, ABSENT], PUBLIC] -> ABSENT @@ -1380,6 +1389,9 @@ PostCommitNonRevertiblePhase stage 1 of 3 with 25 MutationType ops BackReferencedID: 108 RelationIDs: - 107 + *scop.RemoveObjectParent + ObjectID: 108 + ParentSchemaID: 101 *scop.RemoveDroppedColumnType ColumnID: 2 TableID: 108 diff --git a/pkg/sql/schemachanger/scplan/testdata/drop_database b/pkg/sql/schemachanger/scplan/testdata/drop_database index 09076481b486..99615d0b3bee 100644 --- a/pkg/sql/schemachanger/scplan/testdata/drop_database +++ b/pkg/sql/schemachanger/scplan/testdata/drop_database @@ -19,7 +19,7 @@ COMMENT ON TABLE db1.sc1.t1 IS 't1 is good'; ops DROP DATABASE db1 CASCADE ---- -StatementPhase stage 1 of 1 with 43 MutationType ops +StatementPhase stage 1 of 1 with 54 MutationType ops transitions: [[Namespace:{DescID: 104, Name: db1, ReferencedDescID: 0}, ABSENT], PUBLIC] -> ABSENT [[Owner:{DescID: 104}, ABSENT], PUBLIC] -> ABSENT @@ -238,8 +238,14 @@ StatementPhase stage 1 of 1 with 43 MutationType ops SchemaID: 106 *scop.MarkDescriptorAsDropped DescriptorID: 107 + *scop.RemoveObjectParent + ObjectID: 107 + ParentSchemaID: 105 *scop.MarkDescriptorAsDropped DescriptorID: 110 + *scop.RemoveObjectParent + ObjectID: 110 + ParentSchemaID: 105 *scop.RemoveColumnDefaultExpression ColumnID: 3 TableID: 110 @@ -250,8 +256,14 @@ StatementPhase stage 1 of 1 with 43 MutationType ops - 107 *scop.MarkDescriptorAsDropped DescriptorID: 108 + *scop.RemoveObjectParent + ObjectID: 108 + ParentSchemaID: 106 *scop.MarkDescriptorAsDropped DescriptorID: 109 + *scop.RemoveObjectParent + ObjectID: 109 + ParentSchemaID: 106 *scop.RemoveTableComment TableID: 109 *scop.RemoveColumnDefaultExpression @@ -264,33 +276,51 @@ StatementPhase stage 1 of 1 with 43 MutationType ops - 108 *scop.MarkDescriptorAsDropped DescriptorID: 111 - *scop.RemoveViewBackReferencesInRelations - BackReferencedViewID: 111 + *scop.RemoveBackReferencesInRelations + BackReferencedID: 111 RelationIDs: - 109 + *scop.RemoveObjectParent + ObjectID: 111 + ParentSchemaID: 106 *scop.MarkDescriptorAsDropped DescriptorID: 112 - *scop.RemoveViewBackReferencesInRelations - BackReferencedViewID: 112 + *scop.RemoveBackReferencesInRelations + BackReferencedID: 112 RelationIDs: - 111 + *scop.RemoveObjectParent + ObjectID: 112 + ParentSchemaID: 106 *scop.MarkDescriptorAsDropped DescriptorID: 113 - *scop.RemoveViewBackReferencesInRelations - BackReferencedViewID: 113 + *scop.RemoveBackReferencesInRelations + BackReferencedID: 113 RelationIDs: - 111 - 112 + *scop.RemoveObjectParent + ObjectID: 113 + ParentSchemaID: 106 *scop.MarkDescriptorAsDropped DescriptorID: 114 - *scop.RemoveViewBackReferencesInRelations - BackReferencedViewID: 114 + *scop.RemoveBackReferencesInRelations + BackReferencedID: 114 RelationIDs: - 112 + *scop.RemoveObjectParent + ObjectID: 114 + ParentSchemaID: 106 *scop.MarkDescriptorAsDropped DescriptorID: 115 + *scop.RemoveObjectParent + ObjectID: 115 + ParentSchemaID: 106 *scop.MarkDescriptorAsDropped DescriptorID: 116 + *scop.RemoveObjectParent + ObjectID: 116 + ParentSchemaID: 106 *scop.MarkDescriptorAsDropped DescriptorID: 117 *scop.RemoveBackReferenceInTypes @@ -298,10 +328,13 @@ StatementPhase stage 1 of 1 with 43 MutationType ops TypeIDs: - 115 - 116 - *scop.RemoveViewBackReferencesInRelations - BackReferencedViewID: 117 + *scop.RemoveBackReferencesInRelations + BackReferencedID: 117 RelationIDs: - 114 + *scop.RemoveObjectParent + ObjectID: 117 + ParentSchemaID: 106 *scop.DrainDescriptorName Namespace: DescriptorID: 104 @@ -583,7 +616,7 @@ PreCommitPhase stage 1 of 2 with 1 MutationType op ops: *scop.UndoAllInTxnImmediateMutationOpSideEffects {} -PreCommitPhase stage 2 of 2 with 89 MutationType ops +PreCommitPhase stage 2 of 2 with 100 MutationType ops transitions: [[Namespace:{DescID: 104, Name: db1, ReferencedDescID: 0}, ABSENT], PUBLIC] -> ABSENT [[Owner:{DescID: 104}, ABSENT], PUBLIC] -> ABSENT @@ -804,8 +837,14 @@ PreCommitPhase stage 2 of 2 with 89 MutationType ops SchemaID: 106 *scop.MarkDescriptorAsDropped DescriptorID: 107 + *scop.RemoveObjectParent + ObjectID: 107 + ParentSchemaID: 105 *scop.MarkDescriptorAsDropped DescriptorID: 110 + *scop.RemoveObjectParent + ObjectID: 110 + ParentSchemaID: 105 *scop.RemoveColumnDefaultExpression ColumnID: 3 TableID: 110 @@ -816,8 +855,14 @@ PreCommitPhase stage 2 of 2 with 89 MutationType ops - 107 *scop.MarkDescriptorAsDropped DescriptorID: 108 + *scop.RemoveObjectParent + ObjectID: 108 + ParentSchemaID: 106 *scop.MarkDescriptorAsDropped DescriptorID: 109 + *scop.RemoveObjectParent + ObjectID: 109 + ParentSchemaID: 106 *scop.RemoveTableComment TableID: 109 *scop.RemoveColumnDefaultExpression @@ -834,12 +879,18 @@ PreCommitPhase stage 2 of 2 with 89 MutationType ops BackReferencedID: 111 RelationIDs: - 109 + *scop.RemoveObjectParent + ObjectID: 111 + ParentSchemaID: 106 *scop.MarkDescriptorAsDropped DescriptorID: 112 *scop.RemoveBackReferencesInRelations BackReferencedID: 112 RelationIDs: - 111 + *scop.RemoveObjectParent + ObjectID: 112 + ParentSchemaID: 106 *scop.MarkDescriptorAsDropped DescriptorID: 113 *scop.RemoveBackReferencesInRelations @@ -847,16 +898,28 @@ PreCommitPhase stage 2 of 2 with 89 MutationType ops RelationIDs: - 111 - 112 + *scop.RemoveObjectParent + ObjectID: 113 + ParentSchemaID: 106 *scop.MarkDescriptorAsDropped DescriptorID: 114 *scop.RemoveBackReferencesInRelations BackReferencedID: 114 RelationIDs: - 112 + *scop.RemoveObjectParent + ObjectID: 114 + ParentSchemaID: 106 *scop.MarkDescriptorAsDropped DescriptorID: 115 + *scop.RemoveObjectParent + ObjectID: 115 + ParentSchemaID: 106 *scop.MarkDescriptorAsDropped DescriptorID: 116 + *scop.RemoveObjectParent + ObjectID: 116 + ParentSchemaID: 106 *scop.MarkDescriptorAsDropped DescriptorID: 117 *scop.RemoveBackReferenceInTypes @@ -868,6 +931,9 @@ PreCommitPhase stage 2 of 2 with 89 MutationType ops BackReferencedID: 117 RelationIDs: - 114 + *scop.RemoveObjectParent + ObjectID: 117 + ParentSchemaID: 106 *scop.DrainDescriptorName Namespace: DescriptorID: 104 diff --git a/pkg/sql/schemachanger/scplan/testdata/drop_function b/pkg/sql/schemachanger/scplan/testdata/drop_function new file mode 100644 index 000000000000..7855e05fba45 --- /dev/null +++ b/pkg/sql/schemachanger/scplan/testdata/drop_function @@ -0,0 +1,169 @@ +setup +CREATE TABLE t( + a INT PRIMARY KEY, + b INT, + C INT, + INDEX t_idx_b(b), + INDEX t_idx_c(c) +); +CREATE SEQUENCE sq1; +CREATE VIEW v AS SELECT a FROM t; +CREATE TYPE notmyworkday AS ENUM ('Monday', 'Tuesday'); +CREATE FUNCTION f(a notmyworkday) RETURNS INT IMMUTABLE LANGUAGE SQL AS $$ + SELECT a FROM t; + SELECT b FROM t@t_idx_b; + SELECT c FROM t@t_idx_c; + SELECT a FROM v; + SELECT nextval('sq1'); +$$; +---- + +ops +DROP FUNCTION f; +---- +StatementPhase stage 1 of 1 with 4 MutationType ops + transitions: + [[Owner:{DescID: 109}, ABSENT], PUBLIC] -> ABSENT + [[UserPrivileges:{DescID: 109, Name: admin}, ABSENT], PUBLIC] -> ABSENT + [[UserPrivileges:{DescID: 109, Name: root}, ABSENT], PUBLIC] -> ABSENT + [[Function:{DescID: 109}, ABSENT], PUBLIC] -> DROPPED + [[ObjectParent:{DescID: 109, ReferencedDescID: 101}, ABSENT], PUBLIC] -> ABSENT + [[FunctionName:{DescID: 109}, ABSENT], PUBLIC] -> ABSENT + [[FunctionVolatility:{DescID: 109}, ABSENT], PUBLIC] -> ABSENT + [[FunctionLeakProof:{DescID: 109}, ABSENT], PUBLIC] -> ABSENT + [[FunctionNullInputBehavior:{DescID: 109}, ABSENT], PUBLIC] -> ABSENT + [[FunctionBody:{DescID: 109}, ABSENT], PUBLIC] -> ABSENT + ops: + *scop.MarkDescriptorAsDropped + DescriptorID: 109 + *scop.RemoveObjectParent + ObjectID: 109 + ParentSchemaID: 101 + *scop.RemoveBackReferenceInTypes + BackReferencedDescriptorID: 109 + TypeIDs: + - 107 + - 108 + *scop.RemoveBackReferencesInRelations + BackReferencedID: 109 + RelationIDs: + - 104 + - 104 + - 104 + - 106 + - 105 +PreCommitPhase stage 1 of 2 with 1 MutationType op + transitions: + [[Owner:{DescID: 109}, ABSENT], ABSENT] -> PUBLIC + [[UserPrivileges:{DescID: 109, Name: admin}, ABSENT], ABSENT] -> PUBLIC + [[UserPrivileges:{DescID: 109, Name: root}, ABSENT], ABSENT] -> PUBLIC + [[Function:{DescID: 109}, ABSENT], DROPPED] -> PUBLIC + [[ObjectParent:{DescID: 109, ReferencedDescID: 101}, ABSENT], ABSENT] -> PUBLIC + [[FunctionName:{DescID: 109}, ABSENT], ABSENT] -> PUBLIC + [[FunctionVolatility:{DescID: 109}, ABSENT], ABSENT] -> PUBLIC + [[FunctionLeakProof:{DescID: 109}, ABSENT], ABSENT] -> PUBLIC + [[FunctionNullInputBehavior:{DescID: 109}, ABSENT], ABSENT] -> PUBLIC + [[FunctionBody:{DescID: 109}, ABSENT], ABSENT] -> PUBLIC + ops: + *scop.UndoAllInTxnImmediateMutationOpSideEffects + {} +PreCommitPhase stage 2 of 2 with 11 MutationType ops + transitions: + [[Owner:{DescID: 109}, ABSENT], PUBLIC] -> ABSENT + [[UserPrivileges:{DescID: 109, Name: admin}, ABSENT], PUBLIC] -> ABSENT + [[UserPrivileges:{DescID: 109, Name: root}, ABSENT], PUBLIC] -> ABSENT + [[Function:{DescID: 109}, ABSENT], PUBLIC] -> DROPPED + [[ObjectParent:{DescID: 109, ReferencedDescID: 101}, ABSENT], PUBLIC] -> ABSENT + [[FunctionName:{DescID: 109}, ABSENT], PUBLIC] -> ABSENT + [[FunctionVolatility:{DescID: 109}, ABSENT], PUBLIC] -> ABSENT + [[FunctionLeakProof:{DescID: 109}, ABSENT], PUBLIC] -> ABSENT + [[FunctionNullInputBehavior:{DescID: 109}, ABSENT], PUBLIC] -> ABSENT + [[FunctionBody:{DescID: 109}, ABSENT], PUBLIC] -> ABSENT + ops: + *scop.MarkDescriptorAsDropped + DescriptorID: 109 + *scop.RemoveObjectParent + ObjectID: 109 + ParentSchemaID: 101 + *scop.RemoveBackReferenceInTypes + BackReferencedDescriptorID: 109 + TypeIDs: + - 107 + - 108 + *scop.RemoveBackReferencesInRelations + BackReferencedID: 109 + RelationIDs: + - 104 + - 104 + - 104 + - 106 + - 105 + *scop.SetJobStateOnDescriptor + DescriptorID: 104 + Initialize: true + *scop.SetJobStateOnDescriptor + DescriptorID: 105 + Initialize: true + *scop.SetJobStateOnDescriptor + DescriptorID: 106 + Initialize: true + *scop.SetJobStateOnDescriptor + DescriptorID: 107 + Initialize: true + *scop.SetJobStateOnDescriptor + DescriptorID: 108 + Initialize: true + *scop.SetJobStateOnDescriptor + DescriptorID: 109 + Initialize: true + *scop.CreateSchemaChangerJob + Authorization: + UserName: root + DescriptorIDs: + - 104 + - 105 + - 106 + - 107 + - 108 + - 109 + JobID: 1 + NonCancelable: true + RunningStatus: PostCommitNonRevertiblePhase stage 1 of 1 with 1 MutationType op pending + Statements: + - statement: DROP FUNCTION f + redactedstatement: DROP FUNCTION ‹""›.‹""›.‹f› + statementtag: DROP FUNCTION +PostCommitNonRevertiblePhase stage 1 of 1 with 8 MutationType ops + transitions: + [[Function:{DescID: 109}, ABSENT], DROPPED] -> ABSENT + ops: + *scop.DeleteDescriptor + DescriptorID: 109 + *scop.RemoveJobStateFromDescriptor + DescriptorID: 104 + JobID: 1 + *scop.RemoveJobStateFromDescriptor + DescriptorID: 105 + JobID: 1 + *scop.RemoveJobStateFromDescriptor + DescriptorID: 106 + JobID: 1 + *scop.RemoveJobStateFromDescriptor + DescriptorID: 107 + JobID: 1 + *scop.RemoveJobStateFromDescriptor + DescriptorID: 108 + JobID: 1 + *scop.RemoveJobStateFromDescriptor + DescriptorID: 109 + JobID: 1 + *scop.UpdateSchemaChangerJob + DescriptorIDsToRemove: + - 104 + - 105 + - 106 + - 107 + - 108 + - 109 + IsNonCancelable: true + JobID: 1 diff --git a/pkg/sql/schemachanger/scplan/testdata/drop_index b/pkg/sql/schemachanger/scplan/testdata/drop_index index 64a240b31d13..a9c21d1021d9 100644 --- a/pkg/sql/schemachanger/scplan/testdata/drop_index +++ b/pkg/sql/schemachanger/scplan/testdata/drop_index @@ -620,7 +620,7 @@ DROP INDEX idx3 CASCADE ops DROP INDEX idx4 CASCADE ---- -StatementPhase stage 1 of 1 with 4 MutationType ops +StatementPhase stage 1 of 1 with 5 MutationType ops transitions: [[IndexColumn:{DescID: 104, ColumnID: 2, IndexID: 8}, ABSENT], PUBLIC] -> ABSENT [[IndexColumn:{DescID: 104, ColumnID: 3, IndexID: 8}, ABSENT], PUBLIC] -> ABSENT @@ -643,10 +643,13 @@ StatementPhase stage 1 of 1 with 4 MutationType ops ops: *scop.MarkDescriptorAsDropped DescriptorID: 105 - *scop.RemoveViewBackReferencesInRelations - BackReferencedViewID: 105 + *scop.RemoveBackReferencesInRelations + BackReferencedID: 105 RelationIDs: - 104 + *scop.RemoveObjectParent + ObjectID: 105 + ParentSchemaID: 101 *scop.MakePublicSecondaryIndexWriteOnly IndexID: 8 TableID: 104 @@ -679,7 +682,7 @@ PreCommitPhase stage 1 of 2 with 1 MutationType op ops: *scop.UndoAllInTxnImmediateMutationOpSideEffects {} -PreCommitPhase stage 2 of 2 with 10 MutationType ops +PreCommitPhase stage 2 of 2 with 11 MutationType ops transitions: [[IndexColumn:{DescID: 104, ColumnID: 2, IndexID: 8}, ABSENT], PUBLIC] -> ABSENT [[IndexColumn:{DescID: 104, ColumnID: 3, IndexID: 8}, ABSENT], PUBLIC] -> ABSENT @@ -706,6 +709,9 @@ PreCommitPhase stage 2 of 2 with 10 MutationType ops BackReferencedID: 105 RelationIDs: - 104 + *scop.RemoveObjectParent + ObjectID: 105 + ParentSchemaID: 101 *scop.MakePublicSecondaryIndexWriteOnly IndexID: 8 TableID: 104 @@ -958,7 +964,7 @@ DROP INDEX idx4 CASCADE ops DROP INDEX v2@idx CASCADE; ---- -StatementPhase stage 1 of 1 with 5 MutationType ops +StatementPhase stage 1 of 1 with 6 MutationType ops transitions: [[IndexColumn:{DescID: 106, ColumnID: 2, IndexID: 2}, ABSENT], PUBLIC] -> ABSENT [[IndexColumn:{DescID: 106, ColumnID: 3, IndexID: 2}, ABSENT], PUBLIC] -> ABSENT @@ -990,10 +996,13 @@ StatementPhase stage 1 of 1 with 5 MutationType ops ops: *scop.MarkDescriptorAsDropped DescriptorID: 107 - *scop.RemoveViewBackReferencesInRelations - BackReferencedViewID: 107 + *scop.RemoveBackReferencesInRelations + BackReferencedID: 107 RelationIDs: - 106 + *scop.RemoveObjectParent + ObjectID: 107 + ParentSchemaID: 101 *scop.RemoveColumnDefaultExpression ColumnID: 2 TableID: 107 @@ -1038,7 +1047,7 @@ PreCommitPhase stage 1 of 2 with 1 MutationType op ops: *scop.UndoAllInTxnImmediateMutationOpSideEffects {} -PreCommitPhase stage 2 of 2 with 12 MutationType ops +PreCommitPhase stage 2 of 2 with 13 MutationType ops transitions: [[IndexColumn:{DescID: 106, ColumnID: 2, IndexID: 2}, ABSENT], PUBLIC] -> ABSENT [[IndexColumn:{DescID: 106, ColumnID: 3, IndexID: 2}, ABSENT], PUBLIC] -> ABSENT @@ -1074,6 +1083,9 @@ PreCommitPhase stage 2 of 2 with 12 MutationType ops BackReferencedID: 107 RelationIDs: - 106 + *scop.RemoveObjectParent + ObjectID: 107 + ParentSchemaID: 101 *scop.RemoveColumnDefaultExpression ColumnID: 2 TableID: 107 diff --git a/pkg/sql/schemachanger/scplan/testdata/drop_owned_by b/pkg/sql/schemachanger/scplan/testdata/drop_owned_by index 2ba0eb382b13..1113840fe0aa 100644 --- a/pkg/sql/schemachanger/scplan/testdata/drop_owned_by +++ b/pkg/sql/schemachanger/scplan/testdata/drop_owned_by @@ -17,7 +17,7 @@ CREATE VIEW s.v2 AS (SELECT 'a'::s.typ::string AS k, name FROM s.v1); ops DROP OWNED BY r ---- -StatementPhase stage 1 of 1 with 28 MutationType ops +StatementPhase stage 1 of 1 with 36 MutationType ops transitions: [[UserPrivileges:{DescID: 100, Name: r}, ABSENT], PUBLIC] -> ABSENT [[Namespace:{DescID: 105, Name: s, ReferencedDescID: 100}, ABSENT], PUBLIC] -> ABSENT @@ -158,8 +158,14 @@ StatementPhase stage 1 of 1 with 28 MutationType ops User: r *scop.MarkDescriptorAsDropped DescriptorID: 106 + *scop.RemoveObjectParent + ObjectID: 106 + ParentSchemaID: 101 *scop.MarkDescriptorAsDropped DescriptorID: 109 + *scop.RemoveObjectParent + ObjectID: 109 + ParentSchemaID: 101 *scop.RemoveColumnDefaultExpression ColumnID: 3 TableID: 109 @@ -170,8 +176,14 @@ StatementPhase stage 1 of 1 with 28 MutationType ops - 106 *scop.MarkDescriptorAsDropped DescriptorID: 107 + *scop.RemoveObjectParent + ObjectID: 107 + ParentSchemaID: 105 *scop.MarkDescriptorAsDropped DescriptorID: 108 + *scop.RemoveObjectParent + ObjectID: 108 + ParentSchemaID: 105 *scop.RemoveColumnDefaultExpression ColumnID: 3 TableID: 108 @@ -182,14 +194,23 @@ StatementPhase stage 1 of 1 with 28 MutationType ops - 107 *scop.MarkDescriptorAsDropped DescriptorID: 110 - *scop.RemoveViewBackReferencesInRelations - BackReferencedViewID: 110 + *scop.RemoveBackReferencesInRelations + BackReferencedID: 110 RelationIDs: - 108 + *scop.RemoveObjectParent + ObjectID: 110 + ParentSchemaID: 105 *scop.MarkDescriptorAsDropped DescriptorID: 111 + *scop.RemoveObjectParent + ObjectID: 111 + ParentSchemaID: 105 *scop.MarkDescriptorAsDropped DescriptorID: 112 + *scop.RemoveObjectParent + ObjectID: 112 + ParentSchemaID: 105 *scop.MarkDescriptorAsDropped DescriptorID: 113 *scop.RemoveBackReferenceInTypes @@ -197,10 +218,13 @@ StatementPhase stage 1 of 1 with 28 MutationType ops TypeIDs: - 111 - 112 - *scop.RemoveViewBackReferencesInRelations - BackReferencedViewID: 113 + *scop.RemoveBackReferencesInRelations + BackReferencedID: 113 RelationIDs: - 110 + *scop.RemoveObjectParent + ObjectID: 113 + ParentSchemaID: 105 *scop.DrainDescriptorName Namespace: DatabaseID: 100 @@ -383,7 +407,7 @@ PreCommitPhase stage 1 of 2 with 1 MutationType op ops: *scop.UndoAllInTxnImmediateMutationOpSideEffects {} -PreCommitPhase stage 2 of 2 with 57 MutationType ops +PreCommitPhase stage 2 of 2 with 65 MutationType ops transitions: [[UserPrivileges:{DescID: 100, Name: r}, ABSENT], PUBLIC] -> ABSENT [[Namespace:{DescID: 105, Name: s, ReferencedDescID: 100}, ABSENT], PUBLIC] -> ABSENT @@ -524,8 +548,14 @@ PreCommitPhase stage 2 of 2 with 57 MutationType ops User: r *scop.MarkDescriptorAsDropped DescriptorID: 106 + *scop.RemoveObjectParent + ObjectID: 106 + ParentSchemaID: 101 *scop.MarkDescriptorAsDropped DescriptorID: 109 + *scop.RemoveObjectParent + ObjectID: 109 + ParentSchemaID: 101 *scop.RemoveColumnDefaultExpression ColumnID: 3 TableID: 109 @@ -536,8 +566,14 @@ PreCommitPhase stage 2 of 2 with 57 MutationType ops - 106 *scop.MarkDescriptorAsDropped DescriptorID: 107 + *scop.RemoveObjectParent + ObjectID: 107 + ParentSchemaID: 105 *scop.MarkDescriptorAsDropped DescriptorID: 108 + *scop.RemoveObjectParent + ObjectID: 108 + ParentSchemaID: 105 *scop.RemoveColumnDefaultExpression ColumnID: 3 TableID: 108 @@ -552,10 +588,19 @@ PreCommitPhase stage 2 of 2 with 57 MutationType ops BackReferencedID: 110 RelationIDs: - 108 + *scop.RemoveObjectParent + ObjectID: 110 + ParentSchemaID: 105 *scop.MarkDescriptorAsDropped DescriptorID: 111 + *scop.RemoveObjectParent + ObjectID: 111 + ParentSchemaID: 105 *scop.MarkDescriptorAsDropped DescriptorID: 112 + *scop.RemoveObjectParent + ObjectID: 112 + ParentSchemaID: 105 *scop.MarkDescriptorAsDropped DescriptorID: 113 *scop.RemoveBackReferenceInTypes @@ -567,6 +612,9 @@ PreCommitPhase stage 2 of 2 with 57 MutationType ops BackReferencedID: 113 RelationIDs: - 110 + *scop.RemoveObjectParent + ObjectID: 113 + ParentSchemaID: 105 *scop.DrainDescriptorName Namespace: DatabaseID: 100 diff --git a/pkg/sql/schemachanger/scplan/testdata/drop_schema b/pkg/sql/schemachanger/scplan/testdata/drop_schema index 77bb08969ee6..21d956e26138 100644 --- a/pkg/sql/schemachanger/scplan/testdata/drop_schema +++ b/pkg/sql/schemachanger/scplan/testdata/drop_schema @@ -1131,7 +1131,7 @@ DROP SCHEMA defaultdb.SC1 CASCADE ops DROP SCHEMA defaultdb.SC1 CASCADE ---- -StatementPhase stage 1 of 1 with 31 MutationType ops +StatementPhase stage 1 of 1 with 40 MutationType ops transitions: [[Namespace:{DescID: 104, Name: sc1, ReferencedDescID: 100}, ABSENT], PUBLIC] -> ABSENT [[Owner:{DescID: 104}, ABSENT], PUBLIC] -> ABSENT @@ -1291,8 +1291,14 @@ StatementPhase stage 1 of 1 with 31 MutationType ops SchemaID: 104 *scop.MarkDescriptorAsDropped DescriptorID: 105 + *scop.RemoveObjectParent + ObjectID: 105 + ParentSchemaID: 104 *scop.MarkDescriptorAsDropped DescriptorID: 106 + *scop.RemoveObjectParent + ObjectID: 106 + ParentSchemaID: 104 *scop.RemoveTableComment TableID: 106 *scop.RemoveColumnDefaultExpression @@ -1305,33 +1311,51 @@ StatementPhase stage 1 of 1 with 31 MutationType ops - 105 *scop.MarkDescriptorAsDropped DescriptorID: 107 - *scop.RemoveViewBackReferencesInRelations - BackReferencedViewID: 107 + *scop.RemoveBackReferencesInRelations + BackReferencedID: 107 RelationIDs: - 106 + *scop.RemoveObjectParent + ObjectID: 107 + ParentSchemaID: 104 *scop.MarkDescriptorAsDropped DescriptorID: 108 - *scop.RemoveViewBackReferencesInRelations - BackReferencedViewID: 108 + *scop.RemoveBackReferencesInRelations + BackReferencedID: 108 RelationIDs: - 107 + *scop.RemoveObjectParent + ObjectID: 108 + ParentSchemaID: 104 *scop.MarkDescriptorAsDropped DescriptorID: 109 - *scop.RemoveViewBackReferencesInRelations - BackReferencedViewID: 109 + *scop.RemoveBackReferencesInRelations + BackReferencedID: 109 RelationIDs: - 107 - 108 + *scop.RemoveObjectParent + ObjectID: 109 + ParentSchemaID: 104 *scop.MarkDescriptorAsDropped DescriptorID: 110 - *scop.RemoveViewBackReferencesInRelations - BackReferencedViewID: 110 + *scop.RemoveBackReferencesInRelations + BackReferencedID: 110 RelationIDs: - 108 + *scop.RemoveObjectParent + ObjectID: 110 + ParentSchemaID: 104 *scop.MarkDescriptorAsDropped DescriptorID: 111 + *scop.RemoveObjectParent + ObjectID: 111 + ParentSchemaID: 104 *scop.MarkDescriptorAsDropped DescriptorID: 112 + *scop.RemoveObjectParent + ObjectID: 112 + ParentSchemaID: 104 *scop.MarkDescriptorAsDropped DescriptorID: 113 *scop.RemoveBackReferenceInTypes @@ -1339,10 +1363,13 @@ StatementPhase stage 1 of 1 with 31 MutationType ops TypeIDs: - 111 - 112 - *scop.RemoveViewBackReferencesInRelations - BackReferencedViewID: 113 + *scop.RemoveBackReferencesInRelations + BackReferencedID: 113 RelationIDs: - 110 + *scop.RemoveObjectParent + ObjectID: 113 + ParentSchemaID: 104 *scop.DrainDescriptorName Namespace: DatabaseID: 100 @@ -1554,7 +1581,7 @@ PreCommitPhase stage 1 of 2 with 1 MutationType op ops: *scop.UndoAllInTxnImmediateMutationOpSideEffects {} -PreCommitPhase stage 2 of 2 with 68 MutationType ops +PreCommitPhase stage 2 of 2 with 77 MutationType ops transitions: [[Namespace:{DescID: 104, Name: sc1, ReferencedDescID: 100}, ABSENT], PUBLIC] -> ABSENT [[Owner:{DescID: 104}, ABSENT], PUBLIC] -> ABSENT @@ -1714,8 +1741,14 @@ PreCommitPhase stage 2 of 2 with 68 MutationType ops SchemaID: 104 *scop.MarkDescriptorAsDropped DescriptorID: 105 + *scop.RemoveObjectParent + ObjectID: 105 + ParentSchemaID: 104 *scop.MarkDescriptorAsDropped DescriptorID: 106 + *scop.RemoveObjectParent + ObjectID: 106 + ParentSchemaID: 104 *scop.RemoveTableComment TableID: 106 *scop.RemoveColumnDefaultExpression @@ -1732,12 +1765,18 @@ PreCommitPhase stage 2 of 2 with 68 MutationType ops BackReferencedID: 107 RelationIDs: - 106 + *scop.RemoveObjectParent + ObjectID: 107 + ParentSchemaID: 104 *scop.MarkDescriptorAsDropped DescriptorID: 108 *scop.RemoveBackReferencesInRelations BackReferencedID: 108 RelationIDs: - 107 + *scop.RemoveObjectParent + ObjectID: 108 + ParentSchemaID: 104 *scop.MarkDescriptorAsDropped DescriptorID: 109 *scop.RemoveBackReferencesInRelations @@ -1745,16 +1784,28 @@ PreCommitPhase stage 2 of 2 with 68 MutationType ops RelationIDs: - 107 - 108 + *scop.RemoveObjectParent + ObjectID: 109 + ParentSchemaID: 104 *scop.MarkDescriptorAsDropped DescriptorID: 110 *scop.RemoveBackReferencesInRelations BackReferencedID: 110 RelationIDs: - 108 + *scop.RemoveObjectParent + ObjectID: 110 + ParentSchemaID: 104 *scop.MarkDescriptorAsDropped DescriptorID: 111 + *scop.RemoveObjectParent + ObjectID: 111 + ParentSchemaID: 104 *scop.MarkDescriptorAsDropped DescriptorID: 112 + *scop.RemoveObjectParent + ObjectID: 112 + ParentSchemaID: 104 *scop.MarkDescriptorAsDropped DescriptorID: 113 *scop.RemoveBackReferenceInTypes @@ -1766,6 +1817,9 @@ PreCommitPhase stage 2 of 2 with 68 MutationType ops BackReferencedID: 113 RelationIDs: - 110 + *scop.RemoveObjectParent + ObjectID: 113 + ParentSchemaID: 104 *scop.DrainDescriptorName Namespace: DatabaseID: 100 diff --git a/pkg/sql/schemachanger/scplan/testdata/drop_sequence b/pkg/sql/schemachanger/scplan/testdata/drop_sequence index 9814c254e270..b9968a8ff99c 100644 --- a/pkg/sql/schemachanger/scplan/testdata/drop_sequence +++ b/pkg/sql/schemachanger/scplan/testdata/drop_sequence @@ -5,7 +5,7 @@ CREATE SEQUENCE defaultdb.SQ1 ops DROP SEQUENCE defaultdb.SQ1 ---- -StatementPhase stage 1 of 1 with 2 MutationType ops +StatementPhase stage 1 of 1 with 3 MutationType ops transitions: [[Namespace:{DescID: 104, Name: sq1, ReferencedDescID: 100}, ABSENT], PUBLIC] -> ABSENT [[Owner:{DescID: 104}, ABSENT], PUBLIC] -> ABSENT @@ -16,6 +16,9 @@ StatementPhase stage 1 of 1 with 2 MutationType ops ops: *scop.MarkDescriptorAsDropped DescriptorID: 104 + *scop.RemoveObjectParent + ObjectID: 104 + ParentSchemaID: 101 *scop.DrainDescriptorName Namespace: DatabaseID: 100 @@ -33,7 +36,7 @@ PreCommitPhase stage 1 of 2 with 1 MutationType op ops: *scop.UndoAllInTxnImmediateMutationOpSideEffects {} -PreCommitPhase stage 2 of 2 with 4 MutationType ops +PreCommitPhase stage 2 of 2 with 5 MutationType ops transitions: [[Namespace:{DescID: 104, Name: sq1, ReferencedDescID: 100}, ABSENT], PUBLIC] -> ABSENT [[Owner:{DescID: 104}, ABSENT], PUBLIC] -> ABSENT @@ -44,6 +47,9 @@ PreCommitPhase stage 2 of 2 with 4 MutationType ops ops: *scop.MarkDescriptorAsDropped DescriptorID: 104 + *scop.RemoveObjectParent + ObjectID: 104 + ParentSchemaID: 101 *scop.DrainDescriptorName Namespace: DatabaseID: 100 @@ -92,7 +98,7 @@ CREATE TABLE defaultdb.blog_posts2 (id INT8 PRIMARY KEY, val INT8 DEFAULT nextva ops DROP SEQUENCE defaultdb.SQ1 CASCADE ---- -StatementPhase stage 1 of 1 with 6 MutationType ops +StatementPhase stage 1 of 1 with 7 MutationType ops transitions: [[Namespace:{DescID: 104, Name: sq1, ReferencedDescID: 100}, ABSENT], PUBLIC] -> ABSENT [[Owner:{DescID: 104}, ABSENT], PUBLIC] -> ABSENT @@ -105,6 +111,9 @@ StatementPhase stage 1 of 1 with 6 MutationType ops ops: *scop.MarkDescriptorAsDropped DescriptorID: 104 + *scop.RemoveObjectParent + ObjectID: 104 + ParentSchemaID: 101 *scop.RemoveColumnDefaultExpression ColumnID: 2 TableID: 105 @@ -140,7 +149,7 @@ PreCommitPhase stage 1 of 2 with 1 MutationType op ops: *scop.UndoAllInTxnImmediateMutationOpSideEffects {} -PreCommitPhase stage 2 of 2 with 10 MutationType ops +PreCommitPhase stage 2 of 2 with 11 MutationType ops transitions: [[Namespace:{DescID: 104, Name: sq1, ReferencedDescID: 100}, ABSENT], PUBLIC] -> ABSENT [[Owner:{DescID: 104}, ABSENT], PUBLIC] -> ABSENT @@ -153,6 +162,9 @@ PreCommitPhase stage 2 of 2 with 10 MutationType ops ops: *scop.MarkDescriptorAsDropped DescriptorID: 104 + *scop.RemoveObjectParent + ObjectID: 104 + ParentSchemaID: 101 *scop.RemoveColumnDefaultExpression ColumnID: 2 TableID: 105 diff --git a/pkg/sql/schemachanger/scplan/testdata/drop_table b/pkg/sql/schemachanger/scplan/testdata/drop_table index 842ea3011950..4d9fa3d3b8f6 100644 --- a/pkg/sql/schemachanger/scplan/testdata/drop_table +++ b/pkg/sql/schemachanger/scplan/testdata/drop_table @@ -29,7 +29,7 @@ COMMENT ON CONSTRAINT fk_customers ON defaultdb.shipments IS 'customer is not go ops DROP TABLE defaultdb.shipments CASCADE; ---- -StatementPhase stage 1 of 1 with 19 MutationType ops +StatementPhase stage 1 of 1 with 22 MutationType ops transitions: [[Namespace:{DescID: 109, Name: shipments, ReferencedDescID: 100}, ABSENT], PUBLIC] -> ABSENT [[Owner:{DescID: 109}, ABSENT], PUBLIC] -> ABSENT @@ -109,6 +109,9 @@ StatementPhase stage 1 of 1 with 19 MutationType ops ops: *scop.MarkDescriptorAsDropped DescriptorID: 109 + *scop.RemoveObjectParent + ObjectID: 109 + ParentSchemaID: 101 *scop.RemoveTableComment TableID: 109 *scop.RemoveColumnDefaultExpression @@ -142,12 +145,18 @@ StatementPhase stage 1 of 1 with 19 MutationType ops TableID: 109 *scop.MarkDescriptorAsDropped DescriptorID: 110 + *scop.RemoveObjectParent + ObjectID: 110 + ParentSchemaID: 101 *scop.MarkDescriptorAsDropped DescriptorID: 111 - *scop.RemoveViewBackReferencesInRelations - BackReferencedViewID: 111 + *scop.RemoveBackReferencesInRelations + BackReferencedID: 111 RelationIDs: - 109 + *scop.RemoveObjectParent + ObjectID: 111 + ParentSchemaID: 101 *scop.DrainDescriptorName Namespace: DatabaseID: 100 @@ -255,7 +264,7 @@ PreCommitPhase stage 1 of 2 with 1 MutationType op ops: *scop.UndoAllInTxnImmediateMutationOpSideEffects {} -PreCommitPhase stage 2 of 2 with 43 MutationType ops +PreCommitPhase stage 2 of 2 with 46 MutationType ops transitions: [[Namespace:{DescID: 109, Name: shipments, ReferencedDescID: 100}, ABSENT], PUBLIC] -> ABSENT [[Owner:{DescID: 109}, ABSENT], PUBLIC] -> ABSENT @@ -336,6 +345,9 @@ PreCommitPhase stage 2 of 2 with 43 MutationType ops ops: *scop.MarkDescriptorAsDropped DescriptorID: 109 + *scop.RemoveObjectParent + ObjectID: 109 + ParentSchemaID: 101 *scop.RemoveTableComment TableID: 109 *scop.RemoveColumnDefaultExpression @@ -369,12 +381,18 @@ PreCommitPhase stage 2 of 2 with 43 MutationType ops TableID: 109 *scop.MarkDescriptorAsDropped DescriptorID: 110 + *scop.RemoveObjectParent + ObjectID: 110 + ParentSchemaID: 101 *scop.MarkDescriptorAsDropped DescriptorID: 111 *scop.RemoveBackReferencesInRelations BackReferencedID: 111 RelationIDs: - 109 + *scop.RemoveObjectParent + ObjectID: 111 + ParentSchemaID: 101 *scop.DrainDescriptorName Namespace: DatabaseID: 100 @@ -1314,7 +1332,7 @@ CREATE TABLE defaultdb.greeter ( ops DROP TABLE defaultdb.greeter ---- -StatementPhase stage 1 of 1 with 9 MutationType ops +StatementPhase stage 1 of 1 with 10 MutationType ops transitions: [[Namespace:{DescID: 114, Name: greeter, ReferencedDescID: 100}, ABSENT], PUBLIC] -> ABSENT [[Owner:{DescID: 114}, ABSENT], PUBLIC] -> ABSENT @@ -1354,6 +1372,9 @@ StatementPhase stage 1 of 1 with 9 MutationType ops ops: *scop.MarkDescriptorAsDropped DescriptorID: 114 + *scop.RemoveObjectParent + ObjectID: 114 + ParentSchemaID: 101 *scop.RemoveColumnDefaultExpression ColumnID: 1 TableID: 114 @@ -1427,7 +1448,7 @@ PreCommitPhase stage 1 of 2 with 1 MutationType op ops: *scop.UndoAllInTxnImmediateMutationOpSideEffects {} -PreCommitPhase stage 2 of 2 with 20 MutationType ops +PreCommitPhase stage 2 of 2 with 21 MutationType ops transitions: [[Namespace:{DescID: 114, Name: greeter, ReferencedDescID: 100}, ABSENT], PUBLIC] -> ABSENT [[Owner:{DescID: 114}, ABSENT], PUBLIC] -> ABSENT @@ -1468,6 +1489,9 @@ PreCommitPhase stage 2 of 2 with 20 MutationType ops ops: *scop.MarkDescriptorAsDropped DescriptorID: 114 + *scop.RemoveObjectParent + ObjectID: 114 + ParentSchemaID: 101 *scop.RemoveColumnDefaultExpression ColumnID: 1 TableID: 114 diff --git a/pkg/sql/schemachanger/scplan/testdata/drop_type b/pkg/sql/schemachanger/scplan/testdata/drop_type index 00df33ab2ca9..f8f530a52d3e 100644 --- a/pkg/sql/schemachanger/scplan/testdata/drop_type +++ b/pkg/sql/schemachanger/scplan/testdata/drop_type @@ -6,7 +6,7 @@ CREATE TYPE defaultdb.ctyp AS (a INT, b INT) ops DROP TYPE defaultdb.typ ---- -StatementPhase stage 1 of 1 with 4 MutationType ops +StatementPhase stage 1 of 1 with 6 MutationType ops transitions: [[Namespace:{DescID: 104, Name: typ, ReferencedDescID: 100}, ABSENT], PUBLIC] -> ABSENT [[Owner:{DescID: 104}, ABSENT], PUBLIC] -> ABSENT @@ -26,8 +26,14 @@ StatementPhase stage 1 of 1 with 4 MutationType ops ops: *scop.MarkDescriptorAsDropped DescriptorID: 104 + *scop.RemoveObjectParent + ObjectID: 104 + ParentSchemaID: 101 *scop.MarkDescriptorAsDropped DescriptorID: 105 + *scop.RemoveObjectParent + ObjectID: 105 + ParentSchemaID: 101 *scop.DrainDescriptorName Namespace: DatabaseID: 100 @@ -60,7 +66,7 @@ PreCommitPhase stage 1 of 2 with 1 MutationType op ops: *scop.UndoAllInTxnImmediateMutationOpSideEffects {} -PreCommitPhase stage 2 of 2 with 7 MutationType ops +PreCommitPhase stage 2 of 2 with 9 MutationType ops transitions: [[Namespace:{DescID: 104, Name: typ, ReferencedDescID: 100}, ABSENT], PUBLIC] -> ABSENT [[Owner:{DescID: 104}, ABSENT], PUBLIC] -> ABSENT @@ -80,8 +86,14 @@ PreCommitPhase stage 2 of 2 with 7 MutationType ops ops: *scop.MarkDescriptorAsDropped DescriptorID: 104 + *scop.RemoveObjectParent + ObjectID: 104 + ParentSchemaID: 101 *scop.MarkDescriptorAsDropped DescriptorID: 105 + *scop.RemoveObjectParent + ObjectID: 105 + ParentSchemaID: 101 *scop.DrainDescriptorName Namespace: DatabaseID: 100 @@ -202,7 +214,7 @@ DROP TYPE defaultdb.typ ops DROP TYPE defaultdb.ctyp ---- -StatementPhase stage 1 of 1 with 6 MutationType ops +StatementPhase stage 1 of 1 with 8 MutationType ops transitions: [[Namespace:{DescID: 106, Name: ctyp, ReferencedDescID: 100}, ABSENT], PUBLIC] -> ABSENT [[Owner:{DescID: 106}, ABSENT], PUBLIC] -> ABSENT @@ -228,8 +240,14 @@ StatementPhase stage 1 of 1 with 6 MutationType ops ElementType: scpb.CompositeTypeAttrName *scop.NotImplemented ElementType: scpb.CompositeTypeAttrName + *scop.RemoveObjectParent + ObjectID: 106 + ParentSchemaID: 101 *scop.MarkDescriptorAsDropped DescriptorID: 107 + *scop.RemoveObjectParent + ObjectID: 107 + ParentSchemaID: 101 *scop.DrainDescriptorName Namespace: DatabaseID: 100 @@ -264,7 +282,7 @@ PreCommitPhase stage 1 of 2 with 1 MutationType op ops: *scop.UndoAllInTxnImmediateMutationOpSideEffects {} -PreCommitPhase stage 2 of 2 with 9 MutationType ops +PreCommitPhase stage 2 of 2 with 11 MutationType ops transitions: [[Namespace:{DescID: 106, Name: ctyp, ReferencedDescID: 100}, ABSENT], PUBLIC] -> ABSENT [[Owner:{DescID: 106}, ABSENT], PUBLIC] -> ABSENT @@ -290,8 +308,14 @@ PreCommitPhase stage 2 of 2 with 9 MutationType ops ElementType: scpb.CompositeTypeAttrName *scop.NotImplemented ElementType: scpb.CompositeTypeAttrName + *scop.RemoveObjectParent + ObjectID: 106 + ParentSchemaID: 101 *scop.MarkDescriptorAsDropped DescriptorID: 107 + *scop.RemoveObjectParent + ObjectID: 107 + ParentSchemaID: 101 *scop.DrainDescriptorName Namespace: DatabaseID: 100 diff --git a/pkg/sql/schemachanger/scplan/testdata/drop_view b/pkg/sql/schemachanger/scplan/testdata/drop_view index 333e35ad5f00..2279637ed272 100644 --- a/pkg/sql/schemachanger/scplan/testdata/drop_view +++ b/pkg/sql/schemachanger/scplan/testdata/drop_view @@ -6,7 +6,7 @@ CREATE VIEW defaultdb.v1 AS (SELECT name FROM defaultdb.t1); ops DROP VIEW defaultdb.v1 ---- -StatementPhase stage 1 of 1 with 3 MutationType ops +StatementPhase stage 1 of 1 with 4 MutationType ops transitions: [[Namespace:{DescID: 105, Name: v1, ReferencedDescID: 100}, ABSENT], PUBLIC] -> ABSENT [[Owner:{DescID: 105}, ABSENT], PUBLIC] -> ABSENT @@ -26,10 +26,13 @@ StatementPhase stage 1 of 1 with 3 MutationType ops ops: *scop.MarkDescriptorAsDropped DescriptorID: 105 - *scop.RemoveViewBackReferencesInRelations - BackReferencedViewID: 105 + *scop.RemoveBackReferencesInRelations + BackReferencedID: 105 RelationIDs: - 104 + *scop.RemoveObjectParent + ObjectID: 105 + ParentSchemaID: 101 *scop.DrainDescriptorName Namespace: DatabaseID: 100 @@ -56,7 +59,7 @@ PreCommitPhase stage 1 of 2 with 1 MutationType op ops: *scop.UndoAllInTxnImmediateMutationOpSideEffects {} -PreCommitPhase stage 2 of 2 with 9 MutationType ops +PreCommitPhase stage 2 of 2 with 10 MutationType ops transitions: [[Namespace:{DescID: 105, Name: v1, ReferencedDescID: 100}, ABSENT], PUBLIC] -> ABSENT [[Owner:{DescID: 105}, ABSENT], PUBLIC] -> ABSENT @@ -80,6 +83,9 @@ PreCommitPhase stage 2 of 2 with 9 MutationType ops BackReferencedID: 105 RelationIDs: - 104 + *scop.RemoveObjectParent + ObjectID: 105 + ParentSchemaID: 101 *scop.DrainDescriptorName Namespace: DatabaseID: 100 @@ -256,7 +262,7 @@ CREATE VIEW v5 AS (SELECT 'a'::defaultdb.typ::string AS k, n2, n1 from defaultdb ops DROP VIEW defaultdb.v1 CASCADE ---- -StatementPhase stage 1 of 1 with 16 MutationType ops +StatementPhase stage 1 of 1 with 21 MutationType ops transitions: [[Namespace:{DescID: 105, Name: v1, ReferencedDescID: 100}, ABSENT], PUBLIC] -> ABSENT [[Owner:{DescID: 105}, ABSENT], PUBLIC] -> ABSENT @@ -351,29 +357,41 @@ StatementPhase stage 1 of 1 with 16 MutationType ops ops: *scop.MarkDescriptorAsDropped DescriptorID: 105 - *scop.RemoveViewBackReferencesInRelations - BackReferencedViewID: 105 + *scop.RemoveBackReferencesInRelations + BackReferencedID: 105 RelationIDs: - 104 + *scop.RemoveObjectParent + ObjectID: 105 + ParentSchemaID: 101 *scop.MarkDescriptorAsDropped DescriptorID: 106 - *scop.RemoveViewBackReferencesInRelations - BackReferencedViewID: 106 + *scop.RemoveBackReferencesInRelations + BackReferencedID: 106 RelationIDs: - 105 + *scop.RemoveObjectParent + ObjectID: 106 + ParentSchemaID: 101 *scop.MarkDescriptorAsDropped DescriptorID: 107 - *scop.RemoveViewBackReferencesInRelations - BackReferencedViewID: 107 + *scop.RemoveBackReferencesInRelations + BackReferencedID: 107 RelationIDs: - 105 - 106 + *scop.RemoveObjectParent + ObjectID: 107 + ParentSchemaID: 101 *scop.MarkDescriptorAsDropped DescriptorID: 108 - *scop.RemoveViewBackReferencesInRelations - BackReferencedViewID: 108 + *scop.RemoveBackReferencesInRelations + BackReferencedID: 108 RelationIDs: - 106 + *scop.RemoveObjectParent + ObjectID: 108 + ParentSchemaID: 101 *scop.MarkDescriptorAsDropped DescriptorID: 111 *scop.RemoveBackReferenceInTypes @@ -381,10 +399,13 @@ StatementPhase stage 1 of 1 with 16 MutationType ops TypeIDs: - 109 - 110 - *scop.RemoveViewBackReferencesInRelations - BackReferencedViewID: 111 + *scop.RemoveBackReferencesInRelations + BackReferencedID: 111 RelationIDs: - 108 + *scop.RemoveObjectParent + ObjectID: 111 + ParentSchemaID: 101 *scop.DrainDescriptorName Namespace: DatabaseID: 100 @@ -510,7 +531,7 @@ PreCommitPhase stage 1 of 2 with 1 MutationType op ops: *scop.UndoAllInTxnImmediateMutationOpSideEffects {} -PreCommitPhase stage 2 of 2 with 45 MutationType ops +PreCommitPhase stage 2 of 2 with 50 MutationType ops transitions: [[Namespace:{DescID: 105, Name: v1, ReferencedDescID: 100}, ABSENT], PUBLIC] -> ABSENT [[Owner:{DescID: 105}, ABSENT], PUBLIC] -> ABSENT @@ -609,12 +630,18 @@ PreCommitPhase stage 2 of 2 with 45 MutationType ops BackReferencedID: 105 RelationIDs: - 104 + *scop.RemoveObjectParent + ObjectID: 105 + ParentSchemaID: 101 *scop.MarkDescriptorAsDropped DescriptorID: 106 *scop.RemoveBackReferencesInRelations BackReferencedID: 106 RelationIDs: - 105 + *scop.RemoveObjectParent + ObjectID: 106 + ParentSchemaID: 101 *scop.MarkDescriptorAsDropped DescriptorID: 107 *scop.RemoveBackReferencesInRelations @@ -622,12 +649,18 @@ PreCommitPhase stage 2 of 2 with 45 MutationType ops RelationIDs: - 105 - 106 + *scop.RemoveObjectParent + ObjectID: 107 + ParentSchemaID: 101 *scop.MarkDescriptorAsDropped DescriptorID: 108 *scop.RemoveBackReferencesInRelations BackReferencedID: 108 RelationIDs: - 106 + *scop.RemoveObjectParent + ObjectID: 108 + ParentSchemaID: 101 *scop.MarkDescriptorAsDropped DescriptorID: 111 *scop.RemoveBackReferenceInTypes @@ -639,6 +672,9 @@ PreCommitPhase stage 2 of 2 with 45 MutationType ops BackReferencedID: 111 RelationIDs: - 108 + *scop.RemoveObjectParent + ObjectID: 111 + ParentSchemaID: 101 *scop.DrainDescriptorName Namespace: DatabaseID: 100 diff --git a/pkg/sql/schemachanger/screl/attr.go b/pkg/sql/schemachanger/screl/attr.go index 68a50141b17e..1ac8c0bcbd74 100644 --- a/pkg/sql/schemachanger/screl/attr.go +++ b/pkg/sql/schemachanger/screl/attr.go @@ -338,6 +338,27 @@ var elementSchemaOptions = []rel.SchemaOption{ rel.EntityMapping(t((*scpb.TablePartitioning)(nil)), rel.EntityAttr(DescID, "TableID"), ), + rel.EntityMapping(t((*scpb.Function)(nil)), + rel.EntityAttr(DescID, "FunctionID"), + ), + rel.EntityMapping(t((*scpb.FunctionName)(nil)), + rel.EntityAttr(DescID, "FunctionID"), + ), + rel.EntityMapping(t((*scpb.FunctionVolatility)(nil)), + rel.EntityAttr(DescID, "FunctionID"), + ), + rel.EntityMapping(t((*scpb.FunctionLeakProof)(nil)), + rel.EntityAttr(DescID, "FunctionID"), + ), + rel.EntityMapping(t((*scpb.FunctionNullInputBehavior)(nil)), + rel.EntityAttr(DescID, "FunctionID"), + ), + rel.EntityMapping(t((*scpb.FunctionBody)(nil)), + rel.EntityAttr(DescID, "FunctionID"), + ), + rel.EntityMapping(t((*scpb.FunctionParamDefaultExpression)(nil)), + rel.EntityAttr(DescID, "FunctionID"), + ), } // Schema is the schema exported by this package covering the elements of scpb. diff --git a/pkg/sql/schemachanger/screl/scalars.go b/pkg/sql/schemachanger/screl/scalars.go index 241f344ff7d4..e74a44f9d1e2 100644 --- a/pkg/sql/schemachanger/screl/scalars.go +++ b/pkg/sql/schemachanger/screl/scalars.go @@ -120,7 +120,9 @@ func MinVersion(el scpb.Element) clusterversion.Key { return clusterversion.V23_1 case *scpb.IndexColumn, *scpb.EnumTypeValue, *scpb.TableZoneConfig: return clusterversion.V22_2UseDelRangeInGCJob - case *scpb.DatabaseData, *scpb.TableData, *scpb.IndexData, *scpb.TablePartitioning: + case *scpb.DatabaseData, *scpb.TableData, *scpb.IndexData, *scpb.TablePartitioning, + *scpb.Function, *scpb.FunctionName, *scpb.FunctionVolatility, *scpb.FunctionLeakProof, + *scpb.FunctionNullInputBehavior, *scpb.FunctionBody, *scpb.FunctionParamDefaultExpression: return clusterversion.V23_1 default: panic(errors.AssertionFailedf("unknown element %T", el)) diff --git a/pkg/sql/schemachanger/sctest/cumulative.go b/pkg/sql/schemachanger/sctest/cumulative.go index e0c753f39527..4bab53146b19 100644 --- a/pkg/sql/schemachanger/sctest/cumulative.go +++ b/pkg/sql/schemachanger/sctest/cumulative.go @@ -675,6 +675,7 @@ FROM SELECT descriptor_id, create_statement FROM crdb_internal.create_schema_statements UNION ALL SELECT descriptor_id, create_statement FROM crdb_internal.create_statements UNION ALL SELECT descriptor_id, create_statement FROM crdb_internal.create_type_statements + UNION ALL SELECT function_id as descriptor_id, create_statement FROM crdb_internal.create_function_statements ) WHERE descriptor_id IN (SELECT id FROM system.namespace) ORDER BY diff --git a/pkg/sql/schemachanger/sctest_generated_test.go b/pkg/sql/schemachanger/sctest_generated_test.go index 90d219265fc6..e22cc464c4a8 100644 --- a/pkg/sql/schemachanger/sctest_generated_test.go +++ b/pkg/sql/schemachanger/sctest_generated_test.go @@ -445,6 +445,31 @@ func TestRollback_drop_column_with_index(t *testing.T) { defer log.Scope(t).Close(t) sctest.Rollback(t, "pkg/sql/schemachanger/testdata/end_to_end/drop_column_with_index", sctest.SingleNodeCluster) } +func TestEndToEndSideEffects_drop_function(t *testing.T) { + defer leaktest.AfterTest(t)() + defer log.Scope(t).Close(t) + sctest.EndToEndSideEffects(t, "pkg/sql/schemachanger/testdata/end_to_end/drop_function", sctest.SingleNodeCluster) +} +func TestExecuteWithDMLInjection_drop_function(t *testing.T) { + defer leaktest.AfterTest(t)() + defer log.Scope(t).Close(t) + sctest.ExecuteWithDMLInjection(t, "pkg/sql/schemachanger/testdata/end_to_end/drop_function", sctest.SingleNodeCluster) +} +func TestGenerateSchemaChangeCorpus_drop_function(t *testing.T) { + defer leaktest.AfterTest(t)() + defer log.Scope(t).Close(t) + sctest.GenerateSchemaChangeCorpus(t, "pkg/sql/schemachanger/testdata/end_to_end/drop_function", sctest.SingleNodeCluster) +} +func TestPause_drop_function(t *testing.T) { + defer leaktest.AfterTest(t)() + defer log.Scope(t).Close(t) + sctest.Pause(t, "pkg/sql/schemachanger/testdata/end_to_end/drop_function", sctest.SingleNodeCluster) +} +func TestRollback_drop_function(t *testing.T) { + defer leaktest.AfterTest(t)() + defer log.Scope(t).Close(t) + sctest.Rollback(t, "pkg/sql/schemachanger/testdata/end_to_end/drop_function", sctest.SingleNodeCluster) +} func TestEndToEndSideEffects_drop_index_hash_sharded_index(t *testing.T) { defer leaktest.AfterTest(t)() defer log.Scope(t).Close(t) diff --git a/pkg/sql/schemachanger/testdata/end_to_end/drop_function b/pkg/sql/schemachanger/testdata/end_to_end/drop_function new file mode 100644 index 000000000000..9ecf429a857e --- /dev/null +++ b/pkg/sql/schemachanger/testdata/end_to_end/drop_function @@ -0,0 +1,382 @@ +setup +CREATE TABLE t( + a INT PRIMARY KEY, + b INT, + C INT, + INDEX t_idx_b(b), + INDEX t_idx_c(c) +); +CREATE SEQUENCE sq1; +CREATE VIEW v AS SELECT a FROM t; +CREATE TYPE notmyworkday AS ENUM ('Monday', 'Tuesday'); +CREATE FUNCTION f(a notmyworkday) RETURNS INT IMMUTABLE LANGUAGE SQL AS $$ + SELECT a FROM t; + SELECT b FROM t@t_idx_b; + SELECT c FROM t@t_idx_c; + SELECT a FROM v; + SELECT nextval('sq1'); +$$; +CREATE TABLE t2(a notmyworkday); +---- +... ++object {100 101 t} -> 104 ++object {100 101 sq1} -> 105 ++object {100 101 v} -> 106 ++object {100 101 notmyworkday} -> 107 ++object {100 101 _notmyworkday} -> 108 ++object {100 101 t2} -> 110 + +test +DROP FUNCTION f; +---- +begin transaction #1 +# begin StatementPhase +checking for feature: DROP FUNCTION +increment telemetry for sql.schema.drop_function +## StatementPhase stage 1 of 1 with 4 MutationType ops +upsert descriptor #101 + schema: + - functions: + - f: + - overloads: + - - argTypes: + - - family: EnumFamily + - oid: 100107 + - udtMetadata: + - arrayTypeOid: 100108 + - id: 109 + - returnType: + - family: IntFamily + - oid: 20 + - width: 64 + id: 101 + modificationTime: {} + ... + withGrantOption: "2" + version: 2 + - version: "2" + + version: "3" +upsert descriptor #104 + ... + - 1 + id: 106 + - - columnIds: + - - 1 + - id: 109 + - - columnIds: + - - 2 + - id: 109 + - indexId: 2 + - - columnIds: + - - 3 + - id: 109 + - indexId: 3 + families: + - columnIds: + ... + time: {} + unexposedParentSchemaId: 101 + - version: "3" + + version: "4" +upsert descriptor #105 + ... + createAsOfTime: + wallTime: "1640995200000000000" + - dependedOnBy: + - - byId: true + - id: 109 + families: + - columnIds: + ... + start: "1" + unexposedParentSchemaId: 101 + - version: "2" + + version: "3" +upsert descriptor #106 + ... + createAsOfTime: + wallTime: "1640995200000000000" + - dependedOnBy: + - - columnIds: + - - 1 + - id: 109 + dependsOn: + - 104 + ... + time: {} + unexposedParentSchemaId: 101 + - version: "2" + + version: "3" + viewQuery: SELECT a FROM defaultdb.public.t +upsert descriptor #107 + ... + version: 2 + referencingDescriptorIds: + - - 109 + - 110 + - version: "3" + + version: "4" +upsert descriptor #108 + ... + version: 2 + referencingDescriptorIds: + - - 109 + - 110 + - version: "3" + + version: "4" +upsert descriptor #109 + ... + oid: 20 + width: 64 + - version: "1" + + state: DROP + + version: "2" + volatility: IMMUTABLE +# end StatementPhase +# begin PreCommitPhase +## 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 11 MutationType ops +upsert descriptor #101 + schema: + - functions: + - f: + - overloads: + - - argTypes: + - - family: EnumFamily + - oid: 100107 + - udtMetadata: + - arrayTypeOid: 100108 + - id: 109 + - returnType: + - family: IntFamily + - oid: 20 + - width: 64 + id: 101 + modificationTime: {} + ... + withGrantOption: "2" + version: 2 + - version: "2" + + version: "3" +upsert descriptor #104 + ... + createAsOfTime: + wallTime: "1640995200000000000" + + declarativeSchemaChangerState: + + authorization: + + userName: root + + jobId: "1" + dependedOnBy: + - columnIds: + - 1 + id: 106 + - - columnIds: + - - 1 + - id: 109 + - - columnIds: + - - 2 + - id: 109 + - indexId: 2 + - - columnIds: + - - 3 + - id: 109 + - indexId: 3 + families: + - columnIds: + ... + time: {} + unexposedParentSchemaId: 101 + - version: "3" + + version: "4" +upsert descriptor #105 + ... + createAsOfTime: + wallTime: "1640995200000000000" + - dependedOnBy: + - - byId: true + - id: 109 + + declarativeSchemaChangerState: + + authorization: + + userName: root + + jobId: "1" + families: + - columnIds: + ... + start: "1" + unexposedParentSchemaId: 101 + - version: "2" + + version: "3" +upsert descriptor #106 + ... + createAsOfTime: + wallTime: "1640995200000000000" + - dependedOnBy: + - - columnIds: + - - 1 + - id: 109 + + declarativeSchemaChangerState: + + authorization: + + userName: root + + jobId: "1" + dependsOn: + - 104 + ... + time: {} + unexposedParentSchemaId: 101 + - version: "2" + + version: "3" + viewQuery: SELECT a FROM defaultdb.public.t +upsert descriptor #107 + type: + arrayTypeId: 108 + + declarativeSchemaChangerState: + + authorization: + + userName: root + + jobId: "1" + enumMembers: + - logicalRepresentation: Monday + ... + version: 2 + referencingDescriptorIds: + - - 109 + - 110 + - version: "3" + + version: "4" +upsert descriptor #108 + ... + family: ArrayFamily + oid: 100108 + + declarativeSchemaChangerState: + + authorization: + + userName: root + + jobId: "1" + id: 108 + kind: ALIAS + ... + version: 2 + referencingDescriptorIds: + - - 109 + - 110 + - version: "3" + + version: "4" +upsert descriptor #109 + function: + + declarativeSchemaChangerState: + + authorization: + + userName: root + + currentStatuses: + + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: DROP FUNCTION ‹""›.‹""›.‹f› + + statement: DROP FUNCTION f + + statementTag: DROP FUNCTION + + targetRanks: + + targets: + dependsOn: + - 104 + ... + oid: 20 + width: 64 + - version: "1" + + state: DROP + + version: "2" + volatility: IMMUTABLE +persist all catalog changes to storage +create job #1 (non-cancelable: true): "DROP FUNCTION \"\".\"\".f" + descriptor IDs: [104 105 106 107 108 109] +# end PreCommitPhase +commit transaction #1 +notified job registry to adopt jobs: [1] +# begin PostCommitPhase +begin transaction #2 +commit transaction #2 +begin transaction #3 +## PostCommitNonRevertiblePhase stage 1 of 1 with 8 MutationType ops +upsert descriptor #104 + ... + createAsOfTime: + wallTime: "1640995200000000000" + - declarativeSchemaChangerState: + - authorization: + - userName: root + - jobId: "1" + dependedOnBy: + - columnIds: + ... + time: {} + unexposedParentSchemaId: 101 + - version: "4" + + version: "5" +upsert descriptor #105 + ... + createAsOfTime: + wallTime: "1640995200000000000" + - declarativeSchemaChangerState: + - authorization: + - userName: root + - jobId: "1" + families: + - columnIds: + ... + start: "1" + unexposedParentSchemaId: 101 + - version: "3" + + version: "4" +upsert descriptor #106 + ... + createAsOfTime: + wallTime: "1640995200000000000" + - declarativeSchemaChangerState: + - authorization: + - userName: root + - jobId: "1" + dependsOn: + - 104 + ... + time: {} + unexposedParentSchemaId: 101 + - version: "3" + + version: "4" + viewQuery: SELECT a FROM defaultdb.public.t +upsert descriptor #107 + type: + arrayTypeId: 108 + - declarativeSchemaChangerState: + - authorization: + - userName: root + - jobId: "1" + enumMembers: + - logicalRepresentation: Monday + ... + referencingDescriptorIds: + - 110 + - version: "4" + + version: "5" +upsert descriptor #108 + ... + family: ArrayFamily + oid: 100108 + - declarativeSchemaChangerState: + - authorization: + - userName: root + - jobId: "1" + id: 108 + kind: ALIAS + ... + referencingDescriptorIds: + - 110 + - version: "4" + + version: "5" +delete descriptor #109 +persist all catalog changes to storage +update progress of schema change job #1: "all stages completed" +set schema change job #1 to non-cancellable +updated schema change job #1 descriptor IDs to [] +write *eventpb.FinishSchemaChange to event log: + sc: + descriptorId: 109 +commit transaction #3 +# end PostCommitPhase diff --git a/pkg/sql/schemachanger/testdata/end_to_end/drop_index_with_materialized_view_dep b/pkg/sql/schemachanger/testdata/end_to_end/drop_index_with_materialized_view_dep index d13523b6df9e..26927b5c3437 100644 --- a/pkg/sql/schemachanger/testdata/end_to_end/drop_index_with_materialized_view_dep +++ b/pkg/sql/schemachanger/testdata/end_to_end/drop_index_with_materialized_view_dep @@ -44,7 +44,7 @@ write *eventpb.DropIndex to event log: tag: DROP INDEX user: root tableName: defaultdb.public.v2 -## StatementPhase stage 1 of 1 with 5 MutationType ops +## StatementPhase stage 1 of 1 with 6 MutationType ops delete object namespace entry {100 101 v3} -> 106 upsert descriptor #105 ... @@ -127,7 +127,7 @@ upsert descriptor #106 ## 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 12 MutationType ops +## PreCommitPhase stage 2 of 2 with 13 MutationType ops delete object namespace entry {100 101 v3} -> 106 upsert descriptor #105 ... diff --git a/pkg/sql/schemachanger/testdata/end_to_end/drop_table b/pkg/sql/schemachanger/testdata/end_to_end/drop_table index 4bc5b02bfe95..9bdcd3f8e1de 100644 --- a/pkg/sql/schemachanger/testdata/end_to_end/drop_table +++ b/pkg/sql/schemachanger/testdata/end_to_end/drop_table @@ -27,7 +27,7 @@ write *eventpb.DropTable to event log: tag: DROP TABLE user: root tableName: db.sc.t -## StatementPhase stage 1 of 1 with 4 MutationType ops +## StatementPhase stage 1 of 1 with 5 MutationType ops delete object namespace entry {104 106 t} -> 107 upsert descriptor #107 ... @@ -49,7 +49,7 @@ delete comment TableCommentType(objID: 107, subID: 0) ## 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 11 MutationType ops +## PreCommitPhase stage 2 of 2 with 12 MutationType ops delete object namespace entry {104 106 t} -> 107 upsert descriptor #107 ... diff --git a/pkg/sql/schemachanger/testdata/explain/drop_function b/pkg/sql/schemachanger/testdata/explain/drop_function new file mode 100644 index 000000000000..9e9962cb918f --- /dev/null +++ b/pkg/sql/schemachanger/testdata/explain/drop_function @@ -0,0 +1,94 @@ +/* setup */ +CREATE TABLE t( + a INT PRIMARY KEY, + b INT, + C INT, + INDEX t_idx_b(b), + INDEX t_idx_c(c) +); +CREATE SEQUENCE sq1; +CREATE VIEW v AS SELECT a FROM t; +CREATE TYPE notmyworkday AS ENUM ('Monday', 'Tuesday'); +CREATE FUNCTION f(a notmyworkday) RETURNS INT IMMUTABLE LANGUAGE SQL AS $$ + SELECT a FROM t; + SELECT b FROM t@t_idx_b; + SELECT c FROM t@t_idx_c; + SELECT a FROM v; + SELECT nextval('sq1'); +$$; +CREATE TABLE t2(a notmyworkday); + +/* test */ +EXPLAIN (ddl) DROP FUNCTION f; +---- +Schema change plan for DROP FUNCTION ‹""›.‹""›.‹f›; + ├── StatementPhase + │ └── Stage 1 of 1 in StatementPhase + │ ├── 10 elements transitioning toward ABSENT + │ │ ├── PUBLIC → ABSENT Owner:{DescID: 109} + │ │ ├── PUBLIC → ABSENT UserPrivileges:{DescID: 109, Name: admin} + │ │ ├── PUBLIC → ABSENT UserPrivileges:{DescID: 109, Name: root} + │ │ ├── PUBLIC → DROPPED Function:{DescID: 109} + │ │ ├── PUBLIC → ABSENT ObjectParent:{DescID: 109, ReferencedDescID: 101} + │ │ ├── PUBLIC → ABSENT FunctionName:{DescID: 109} + │ │ ├── PUBLIC → ABSENT FunctionVolatility:{DescID: 109} + │ │ ├── PUBLIC → ABSENT FunctionLeakProof:{DescID: 109} + │ │ ├── PUBLIC → ABSENT FunctionNullInputBehavior:{DescID: 109} + │ │ └── PUBLIC → ABSENT FunctionBody:{DescID: 109} + │ └── 4 Mutation operations + │ ├── MarkDescriptorAsDropped {"DescriptorID":109} + │ ├── RemoveObjectParent {"ObjectID":109,"ParentSchemaID":101} + │ ├── RemoveBackReferenceInTypes {"BackReferencedDescriptorID":109} + │ └── RemoveBackReferencesInRelations {"BackReferencedID":109} + ├── PreCommitPhase + │ ├── Stage 1 of 2 in PreCommitPhase + │ │ ├── 10 elements transitioning toward ABSENT + │ │ │ ├── ABSENT → PUBLIC Owner:{DescID: 109} + │ │ │ ├── ABSENT → PUBLIC UserPrivileges:{DescID: 109, Name: admin} + │ │ │ ├── ABSENT → PUBLIC UserPrivileges:{DescID: 109, Name: root} + │ │ │ ├── DROPPED → PUBLIC Function:{DescID: 109} + │ │ │ ├── ABSENT → PUBLIC ObjectParent:{DescID: 109, ReferencedDescID: 101} + │ │ │ ├── ABSENT → PUBLIC FunctionName:{DescID: 109} + │ │ │ ├── ABSENT → PUBLIC FunctionVolatility:{DescID: 109} + │ │ │ ├── ABSENT → PUBLIC FunctionLeakProof:{DescID: 109} + │ │ │ ├── ABSENT → PUBLIC FunctionNullInputBehavior:{DescID: 109} + │ │ │ └── ABSENT → PUBLIC FunctionBody:{DescID: 109} + │ │ └── 1 Mutation operation + │ │ └── UndoAllInTxnImmediateMutationOpSideEffects + │ └── Stage 2 of 2 in PreCommitPhase + │ ├── 10 elements transitioning toward ABSENT + │ │ ├── PUBLIC → ABSENT Owner:{DescID: 109} + │ │ ├── PUBLIC → ABSENT UserPrivileges:{DescID: 109, Name: admin} + │ │ ├── PUBLIC → ABSENT UserPrivileges:{DescID: 109, Name: root} + │ │ ├── PUBLIC → DROPPED Function:{DescID: 109} + │ │ ├── PUBLIC → ABSENT ObjectParent:{DescID: 109, ReferencedDescID: 101} + │ │ ├── PUBLIC → ABSENT FunctionName:{DescID: 109} + │ │ ├── PUBLIC → ABSENT FunctionVolatility:{DescID: 109} + │ │ ├── PUBLIC → ABSENT FunctionLeakProof:{DescID: 109} + │ │ ├── PUBLIC → ABSENT FunctionNullInputBehavior:{DescID: 109} + │ │ └── PUBLIC → ABSENT FunctionBody:{DescID: 109} + │ └── 11 Mutation operations + │ ├── MarkDescriptorAsDropped {"DescriptorID":109} + │ ├── RemoveObjectParent {"ObjectID":109,"ParentSchemaID":101} + │ ├── RemoveBackReferenceInTypes {"BackReferencedDescriptorID":109} + │ ├── RemoveBackReferencesInRelations {"BackReferencedID":109} + │ ├── SetJobStateOnDescriptor {"DescriptorID":104,"Initialize":true} + │ ├── SetJobStateOnDescriptor {"DescriptorID":105,"Initialize":true} + │ ├── SetJobStateOnDescriptor {"DescriptorID":106,"Initialize":true} + │ ├── SetJobStateOnDescriptor {"DescriptorID":107,"Initialize":true} + │ ├── SetJobStateOnDescriptor {"DescriptorID":108,"Initialize":true} + │ ├── SetJobStateOnDescriptor {"DescriptorID":109,"Initialize":true} + │ └── CreateSchemaChangerJob {"NonCancelable":true,"RunningStatus":"PostCommitNonRev..."} + └── PostCommitNonRevertiblePhase + └── Stage 1 of 1 in PostCommitNonRevertiblePhase + ├── 1 element transitioning toward ABSENT + │ └── DROPPED → ABSENT Function:{DescID: 109} + └── 8 Mutation operations + ├── DeleteDescriptor {"DescriptorID":109} + ├── RemoveJobStateFromDescriptor {"DescriptorID":104} + ├── RemoveJobStateFromDescriptor {"DescriptorID":105} + ├── RemoveJobStateFromDescriptor {"DescriptorID":106} + ├── RemoveJobStateFromDescriptor {"DescriptorID":107} + ├── RemoveJobStateFromDescriptor {"DescriptorID":108} + ├── RemoveJobStateFromDescriptor {"DescriptorID":109} + └── UpdateSchemaChangerJob {"IsNonCancelable":true,"RunningStatus":"all stages compl..."} diff --git a/pkg/sql/schemachanger/testdata/explain/drop_index_with_materialized_view_dep b/pkg/sql/schemachanger/testdata/explain/drop_index_with_materialized_view_dep index 8545dacb89a4..df43c258bc84 100644 --- a/pkg/sql/schemachanger/testdata/explain/drop_index_with_materialized_view_dep +++ b/pkg/sql/schemachanger/testdata/explain/drop_index_with_materialized_view_dep @@ -38,9 +38,10 @@ Schema change plan for DROP INDEX ‹defaultdb›.‹public›.‹v2›@‹idx │ │ ├── PUBLIC → ABSENT IndexColumn:{DescID: 106, ColumnID: 1, IndexID: 1} │ │ ├── PUBLIC → VALIDATED PrimaryIndex:{DescID: 106, IndexID: 1, ConstraintID: 1} │ │ └── PUBLIC → ABSENT IndexName:{DescID: 106, Name: v3_pkey, IndexID: 1} - │ └── 5 Mutation operations + │ └── 6 Mutation operations │ ├── MarkDescriptorAsDropped {"DescriptorID":106} - │ ├── RemoveViewBackReferencesInRelations {"BackReferencedViewID":106} + │ ├── RemoveBackReferencesInRelations {"BackReferencedID":106} + │ ├── RemoveObjectParent {"ObjectID":106,"ParentSchemaID":101} │ ├── RemoveColumnDefaultExpression {"ColumnID":2,"TableID":106} │ ├── MakePublicSecondaryIndexWriteOnly {"IndexID":2,"TableID":105} │ └── DrainDescriptorName {"Namespace":{"DatabaseID":100,"DescriptorID":106,"Name":"v3","SchemaID":101}} @@ -105,9 +106,10 @@ Schema change plan for DROP INDEX ‹defaultdb›.‹public›.‹v2›@‹idx │ │ ├── PUBLIC → ABSENT IndexColumn:{DescID: 106, ColumnID: 1, IndexID: 1} │ │ ├── PUBLIC → ABSENT PrimaryIndex:{DescID: 106, IndexID: 1, ConstraintID: 1} │ │ └── PUBLIC → ABSENT IndexName:{DescID: 106, Name: v3_pkey, IndexID: 1} - │ └── 12 Mutation operations + │ └── 13 Mutation operations │ ├── MarkDescriptorAsDropped {"DescriptorID":106} │ ├── RemoveBackReferencesInRelations {"BackReferencedID":106} + │ ├── RemoveObjectParent {"ObjectID":106,"ParentSchemaID":101} │ ├── RemoveColumnDefaultExpression {"ColumnID":2,"TableID":106} │ ├── MakePublicSecondaryIndexWriteOnly {"IndexID":2,"TableID":105} │ ├── DrainDescriptorName {"Namespace":{"DatabaseID":100,"DescriptorID":106,"Name":"v3","SchemaID":101}} diff --git a/pkg/sql/schemachanger/testdata/explain/drop_table b/pkg/sql/schemachanger/testdata/explain/drop_table index 11c25f66bb4e..e8d04dce7172 100644 --- a/pkg/sql/schemachanger/testdata/explain/drop_table +++ b/pkg/sql/schemachanger/testdata/explain/drop_table @@ -41,8 +41,9 @@ Schema change plan for DROP TABLE ‹db›.‹sc›.‹t›; │ │ ├── PUBLIC → ABSENT IndexColumn:{DescID: 107, ColumnID: 2, IndexID: 1} │ │ ├── PUBLIC → VALIDATED PrimaryIndex:{DescID: 107, IndexID: 1, ConstraintID: 1} │ │ └── PUBLIC → ABSENT IndexName:{DescID: 107, Name: t_pkey, IndexID: 1} - │ └── 4 Mutation operations + │ └── 5 Mutation operations │ ├── MarkDescriptorAsDropped {"DescriptorID":107} + │ ├── RemoveObjectParent {"ObjectID":107,"ParentSchemaID":106} │ ├── RemoveTableComment {"TableID":107} │ ├── RemoveColumnDefaultExpression {"ColumnID":3,"TableID":107} │ └── DrainDescriptorName {"Namespace":{"DatabaseID":104,"DescriptorID":107,"Name":"t","SchemaID":106}} @@ -111,8 +112,9 @@ Schema change plan for DROP TABLE ‹db›.‹sc›.‹t›; │ │ ├── PUBLIC → ABSENT IndexColumn:{DescID: 107, ColumnID: 2, IndexID: 1} │ │ ├── PUBLIC → ABSENT PrimaryIndex:{DescID: 107, IndexID: 1, ConstraintID: 1} │ │ └── PUBLIC → ABSENT IndexName:{DescID: 107, Name: t_pkey, IndexID: 1} - │ └── 11 Mutation operations + │ └── 12 Mutation operations │ ├── MarkDescriptorAsDropped {"DescriptorID":107} + │ ├── RemoveObjectParent {"ObjectID":107,"ParentSchemaID":106} │ ├── RemoveTableComment {"TableID":107} │ ├── RemoveColumnDefaultExpression {"ColumnID":3,"TableID":107} │ ├── DrainDescriptorName {"Namespace":{"DatabaseID":104,"DescriptorID":107,"Name":"t","SchemaID":106}} diff --git a/pkg/sql/schemachanger/testdata/explain_verbose/drop_function b/pkg/sql/schemachanger/testdata/explain_verbose/drop_function new file mode 100644 index 000000000000..27dc014ea258 --- /dev/null +++ b/pkg/sql/schemachanger/testdata/explain_verbose/drop_function @@ -0,0 +1,374 @@ +/* setup */ +CREATE TABLE t( + a INT PRIMARY KEY, + b INT, + C INT, + INDEX t_idx_b(b), + INDEX t_idx_c(c) +); +CREATE SEQUENCE sq1; +CREATE VIEW v AS SELECT a FROM t; +CREATE TYPE notmyworkday AS ENUM ('Monday', 'Tuesday'); +CREATE FUNCTION f(a notmyworkday) RETURNS INT IMMUTABLE LANGUAGE SQL AS $$ + SELECT a FROM t; + SELECT b FROM t@t_idx_b; + SELECT c FROM t@t_idx_c; + SELECT a FROM v; + SELECT nextval('sq1'); +$$; +CREATE TABLE t2(a notmyworkday); + +/* test */ +EXPLAIN (ddl, verbose) DROP FUNCTION f; +---- +• Schema change plan for DROP FUNCTION ‹""›.‹""›.‹f›; +│ +├── • StatementPhase +│ │ +│ └── • Stage 1 of 1 in StatementPhase +│ │ +│ ├── • 10 elements transitioning toward ABSENT +│ │ │ +│ │ ├── • Owner:{DescID: 109} +│ │ │ │ PUBLIC → ABSENT +│ │ │ │ +│ │ │ ├── • Precedence dependency from DROPPED Function:{DescID: 109} +│ │ │ │ rule: "descriptor dropped before dependent element removal" +│ │ │ │ +│ │ │ └── • skip PUBLIC → ABSENT operations +│ │ │ rule: "skip element removal ops on descriptor drop" +│ │ │ +│ │ ├── • UserPrivileges:{DescID: 109, Name: admin} +│ │ │ │ PUBLIC → ABSENT +│ │ │ │ +│ │ │ ├── • Precedence dependency from DROPPED Function:{DescID: 109} +│ │ │ │ rule: "descriptor dropped before dependent element removal" +│ │ │ │ +│ │ │ └── • skip PUBLIC → ABSENT operations +│ │ │ rule: "skip element removal ops on descriptor drop" +│ │ │ +│ │ ├── • UserPrivileges:{DescID: 109, Name: root} +│ │ │ │ PUBLIC → ABSENT +│ │ │ │ +│ │ │ ├── • Precedence dependency from DROPPED Function:{DescID: 109} +│ │ │ │ rule: "descriptor dropped before dependent element removal" +│ │ │ │ +│ │ │ └── • skip PUBLIC → ABSENT operations +│ │ │ rule: "skip element removal ops on descriptor drop" +│ │ │ +│ │ ├── • Function:{DescID: 109} +│ │ │ PUBLIC → DROPPED +│ │ │ +│ │ ├── • ObjectParent:{DescID: 109, ReferencedDescID: 101} +│ │ │ │ PUBLIC → ABSENT +│ │ │ │ +│ │ │ └── • Precedence dependency from DROPPED Function:{DescID: 109} +│ │ │ rule: "descriptor dropped before dependent element removal" +│ │ │ +│ │ ├── • FunctionName:{DescID: 109} +│ │ │ │ PUBLIC → ABSENT +│ │ │ │ +│ │ │ ├── • Precedence dependency from DROPPED Function:{DescID: 109} +│ │ │ │ rule: "descriptor dropped before dependent element removal" +│ │ │ │ +│ │ │ └── • skip PUBLIC → ABSENT operations +│ │ │ rule: "skip element removal ops on descriptor drop" +│ │ │ +│ │ ├── • FunctionVolatility:{DescID: 109} +│ │ │ │ PUBLIC → ABSENT +│ │ │ │ +│ │ │ ├── • Precedence dependency from DROPPED Function:{DescID: 109} +│ │ │ │ rule: "descriptor dropped before dependent element removal" +│ │ │ │ +│ │ │ └── • skip PUBLIC → ABSENT operations +│ │ │ rule: "skip element removal ops on descriptor drop" +│ │ │ +│ │ ├── • FunctionLeakProof:{DescID: 109} +│ │ │ │ PUBLIC → ABSENT +│ │ │ │ +│ │ │ ├── • Precedence dependency from DROPPED Function:{DescID: 109} +│ │ │ │ rule: "descriptor dropped before dependent element removal" +│ │ │ │ +│ │ │ └── • skip PUBLIC → ABSENT operations +│ │ │ rule: "skip element removal ops on descriptor drop" +│ │ │ +│ │ ├── • FunctionNullInputBehavior:{DescID: 109} +│ │ │ │ PUBLIC → ABSENT +│ │ │ │ +│ │ │ ├── • Precedence dependency from DROPPED Function:{DescID: 109} +│ │ │ │ rule: "descriptor dropped before dependent element removal" +│ │ │ │ +│ │ │ └── • skip PUBLIC → ABSENT operations +│ │ │ rule: "skip element removal ops on descriptor drop" +│ │ │ +│ │ └── • FunctionBody:{DescID: 109} +│ │ │ PUBLIC → ABSENT +│ │ │ +│ │ └── • Precedence dependency from DROPPED Function:{DescID: 109} +│ │ rule: "descriptor dropped before dependent element removal" +│ │ +│ └── • 4 Mutation operations +│ │ +│ ├── • MarkDescriptorAsDropped +│ │ DescriptorID: 109 +│ │ +│ ├── • RemoveObjectParent +│ │ ObjectID: 109 +│ │ ParentSchemaID: 101 +│ │ +│ ├── • RemoveBackReferenceInTypes +│ │ BackReferencedDescriptorID: 109 +│ │ TypeIDs: +│ │ - 107 +│ │ - 108 +│ │ +│ └── • RemoveBackReferencesInRelations +│ BackReferencedID: 109 +│ RelationIDs: +│ - 104 +│ - 104 +│ - 104 +│ - 106 +│ - 105 +│ +├── • PreCommitPhase +│ │ +│ ├── • Stage 1 of 2 in PreCommitPhase +│ │ │ +│ │ ├── • 10 elements transitioning toward ABSENT +│ │ │ │ +│ │ │ ├── • Owner:{DescID: 109} +│ │ │ │ ABSENT → PUBLIC +│ │ │ │ +│ │ │ ├── • UserPrivileges:{DescID: 109, Name: admin} +│ │ │ │ ABSENT → PUBLIC +│ │ │ │ +│ │ │ ├── • UserPrivileges:{DescID: 109, Name: root} +│ │ │ │ ABSENT → PUBLIC +│ │ │ │ +│ │ │ ├── • Function:{DescID: 109} +│ │ │ │ DROPPED → PUBLIC +│ │ │ │ +│ │ │ ├── • ObjectParent:{DescID: 109, ReferencedDescID: 101} +│ │ │ │ ABSENT → PUBLIC +│ │ │ │ +│ │ │ ├── • FunctionName:{DescID: 109} +│ │ │ │ ABSENT → PUBLIC +│ │ │ │ +│ │ │ ├── • FunctionVolatility:{DescID: 109} +│ │ │ │ ABSENT → PUBLIC +│ │ │ │ +│ │ │ ├── • FunctionLeakProof:{DescID: 109} +│ │ │ │ ABSENT → PUBLIC +│ │ │ │ +│ │ │ ├── • FunctionNullInputBehavior:{DescID: 109} +│ │ │ │ ABSENT → PUBLIC +│ │ │ │ +│ │ │ └── • FunctionBody:{DescID: 109} +│ │ │ ABSENT → PUBLIC +│ │ │ +│ │ └── • 1 Mutation operation +│ │ │ +│ │ └── • UndoAllInTxnImmediateMutationOpSideEffects +│ │ {} +│ │ +│ └── • Stage 2 of 2 in PreCommitPhase +│ │ +│ ├── • 10 elements transitioning toward ABSENT +│ │ │ +│ │ ├── • Owner:{DescID: 109} +│ │ │ │ PUBLIC → ABSENT +│ │ │ │ +│ │ │ ├── • Precedence dependency from DROPPED Function:{DescID: 109} +│ │ │ │ rule: "descriptor dropped before dependent element removal" +│ │ │ │ +│ │ │ └── • skip PUBLIC → ABSENT operations +│ │ │ rule: "skip element removal ops on descriptor drop" +│ │ │ +│ │ ├── • UserPrivileges:{DescID: 109, Name: admin} +│ │ │ │ PUBLIC → ABSENT +│ │ │ │ +│ │ │ ├── • Precedence dependency from DROPPED Function:{DescID: 109} +│ │ │ │ rule: "descriptor dropped before dependent element removal" +│ │ │ │ +│ │ │ └── • skip PUBLIC → ABSENT operations +│ │ │ rule: "skip element removal ops on descriptor drop" +│ │ │ +│ │ ├── • UserPrivileges:{DescID: 109, Name: root} +│ │ │ │ PUBLIC → ABSENT +│ │ │ │ +│ │ │ ├── • Precedence dependency from DROPPED Function:{DescID: 109} +│ │ │ │ rule: "descriptor dropped before dependent element removal" +│ │ │ │ +│ │ │ └── • skip PUBLIC → ABSENT operations +│ │ │ rule: "skip element removal ops on descriptor drop" +│ │ │ +│ │ ├── • Function:{DescID: 109} +│ │ │ PUBLIC → DROPPED +│ │ │ +│ │ ├── • ObjectParent:{DescID: 109, ReferencedDescID: 101} +│ │ │ │ PUBLIC → ABSENT +│ │ │ │ +│ │ │ └── • Precedence dependency from DROPPED Function:{DescID: 109} +│ │ │ rule: "descriptor dropped before dependent element removal" +│ │ │ +│ │ ├── • FunctionName:{DescID: 109} +│ │ │ │ PUBLIC → ABSENT +│ │ │ │ +│ │ │ ├── • Precedence dependency from DROPPED Function:{DescID: 109} +│ │ │ │ rule: "descriptor dropped before dependent element removal" +│ │ │ │ +│ │ │ └── • skip PUBLIC → ABSENT operations +│ │ │ rule: "skip element removal ops on descriptor drop" +│ │ │ +│ │ ├── • FunctionVolatility:{DescID: 109} +│ │ │ │ PUBLIC → ABSENT +│ │ │ │ +│ │ │ ├── • Precedence dependency from DROPPED Function:{DescID: 109} +│ │ │ │ rule: "descriptor dropped before dependent element removal" +│ │ │ │ +│ │ │ └── • skip PUBLIC → ABSENT operations +│ │ │ rule: "skip element removal ops on descriptor drop" +│ │ │ +│ │ ├── • FunctionLeakProof:{DescID: 109} +│ │ │ │ PUBLIC → ABSENT +│ │ │ │ +│ │ │ ├── • Precedence dependency from DROPPED Function:{DescID: 109} +│ │ │ │ rule: "descriptor dropped before dependent element removal" +│ │ │ │ +│ │ │ └── • skip PUBLIC → ABSENT operations +│ │ │ rule: "skip element removal ops on descriptor drop" +│ │ │ +│ │ ├── • FunctionNullInputBehavior:{DescID: 109} +│ │ │ │ PUBLIC → ABSENT +│ │ │ │ +│ │ │ ├── • Precedence dependency from DROPPED Function:{DescID: 109} +│ │ │ │ rule: "descriptor dropped before dependent element removal" +│ │ │ │ +│ │ │ └── • skip PUBLIC → ABSENT operations +│ │ │ rule: "skip element removal ops on descriptor drop" +│ │ │ +│ │ └── • FunctionBody:{DescID: 109} +│ │ │ PUBLIC → ABSENT +│ │ │ +│ │ └── • Precedence dependency from DROPPED Function:{DescID: 109} +│ │ rule: "descriptor dropped before dependent element removal" +│ │ +│ └── • 11 Mutation operations +│ │ +│ ├── • MarkDescriptorAsDropped +│ │ DescriptorID: 109 +│ │ +│ ├── • RemoveObjectParent +│ │ ObjectID: 109 +│ │ ParentSchemaID: 101 +│ │ +│ ├── • RemoveBackReferenceInTypes +│ │ BackReferencedDescriptorID: 109 +│ │ TypeIDs: +│ │ - 107 +│ │ - 108 +│ │ +│ ├── • RemoveBackReferencesInRelations +│ │ BackReferencedID: 109 +│ │ RelationIDs: +│ │ - 104 +│ │ - 104 +│ │ - 104 +│ │ - 106 +│ │ - 105 +│ │ +│ ├── • SetJobStateOnDescriptor +│ │ DescriptorID: 104 +│ │ Initialize: true +│ │ +│ ├── • SetJobStateOnDescriptor +│ │ DescriptorID: 105 +│ │ Initialize: true +│ │ +│ ├── • SetJobStateOnDescriptor +│ │ DescriptorID: 106 +│ │ Initialize: true +│ │ +│ ├── • SetJobStateOnDescriptor +│ │ DescriptorID: 107 +│ │ Initialize: true +│ │ +│ ├── • SetJobStateOnDescriptor +│ │ DescriptorID: 108 +│ │ Initialize: true +│ │ +│ ├── • SetJobStateOnDescriptor +│ │ DescriptorID: 109 +│ │ Initialize: true +│ │ +│ └── • CreateSchemaChangerJob +│ Authorization: +│ UserName: root +│ DescriptorIDs: +│ - 104 +│ - 105 +│ - 106 +│ - 107 +│ - 108 +│ - 109 +│ JobID: 1 +│ NonCancelable: true +│ RunningStatus: PostCommitNonRevertiblePhase stage 1 of 1 with 1 MutationType op pending +│ Statements: +│ - statement: DROP FUNCTION f +│ redactedstatement: DROP FUNCTION ‹""›.‹""›.‹f› +│ statementtag: DROP FUNCTION +│ +└── • PostCommitNonRevertiblePhase + │ + └── • Stage 1 of 1 in PostCommitNonRevertiblePhase + │ + ├── • 1 element transitioning toward ABSENT + │ │ + │ └── • Function:{DescID: 109} + │ │ DROPPED → ABSENT + │ │ + │ └── • PreviousStagePrecedence dependency from DROPPED Function:{DescID: 109} + │ rule: "descriptor dropped in transaction before removal" + │ + └── • 8 Mutation operations + │ + ├── • DeleteDescriptor + │ DescriptorID: 109 + │ + ├── • RemoveJobStateFromDescriptor + │ DescriptorID: 104 + │ JobID: 1 + │ + ├── • RemoveJobStateFromDescriptor + │ DescriptorID: 105 + │ JobID: 1 + │ + ├── • RemoveJobStateFromDescriptor + │ DescriptorID: 106 + │ JobID: 1 + │ + ├── • RemoveJobStateFromDescriptor + │ DescriptorID: 107 + │ JobID: 1 + │ + ├── • RemoveJobStateFromDescriptor + │ DescriptorID: 108 + │ JobID: 1 + │ + ├── • RemoveJobStateFromDescriptor + │ DescriptorID: 109 + │ JobID: 1 + │ + └── • UpdateSchemaChangerJob + DescriptorIDsToRemove: + - 104 + - 105 + - 106 + - 107 + - 108 + - 109 + IsNonCancelable: true + JobID: 1 + RunningStatus: all stages completed diff --git a/pkg/sql/schemachanger/testdata/explain_verbose/drop_index_with_materialized_view_dep b/pkg/sql/schemachanger/testdata/explain_verbose/drop_index_with_materialized_view_dep index 366b5a323e56..bdeb35163bac 100644 --- a/pkg/sql/schemachanger/testdata/explain_verbose/drop_index_with_materialized_view_dep +++ b/pkg/sql/schemachanger/testdata/explain_verbose/drop_index_with_materialized_view_dep @@ -278,16 +278,20 @@ EXPLAIN (ddl, verbose) DROP INDEX idx CASCADE; │ │ └── • skip PUBLIC → ABSENT operations │ │ rule: "skip index dependents removal ops on relation drop" │ │ -│ └── • 5 Mutation operations +│ └── • 6 Mutation operations │ │ │ ├── • MarkDescriptorAsDropped │ │ DescriptorID: 106 │ │ -│ ├── • RemoveViewBackReferencesInRelations -│ │ BackReferencedViewID: 106 +│ ├── • RemoveBackReferencesInRelations +│ │ BackReferencedID: 106 │ │ RelationIDs: │ │ - 105 │ │ +│ ├── • RemoveObjectParent +│ │ ObjectID: 106 +│ │ ParentSchemaID: 101 +│ │ │ ├── • RemoveColumnDefaultExpression │ │ ColumnID: 2 │ │ TableID: 106 @@ -725,7 +729,7 @@ EXPLAIN (ddl, verbose) DROP INDEX idx CASCADE; │ │ └── • skip PUBLIC → ABSENT operations │ │ rule: "skip index dependents removal ops on relation drop" │ │ -│ └── • 12 Mutation operations +│ └── • 13 Mutation operations │ │ │ ├── • MarkDescriptorAsDropped │ │ DescriptorID: 106 @@ -735,6 +739,10 @@ EXPLAIN (ddl, verbose) DROP INDEX idx CASCADE; │ │ RelationIDs: │ │ - 105 │ │ +│ ├── • RemoveObjectParent +│ │ ObjectID: 106 +│ │ ParentSchemaID: 101 +│ │ │ ├── • RemoveColumnDefaultExpression │ │ ColumnID: 2 │ │ TableID: 106 diff --git a/pkg/sql/schemachanger/testdata/explain_verbose/drop_table b/pkg/sql/schemachanger/testdata/explain_verbose/drop_table index 6ab899cba17d..777bc98f980b 100644 --- a/pkg/sql/schemachanger/testdata/explain_verbose/drop_table +++ b/pkg/sql/schemachanger/testdata/explain_verbose/drop_table @@ -304,11 +304,15 @@ EXPLAIN (ddl, verbose) DROP TABLE db.sc.t; │ │ └── • skip PUBLIC → ABSENT operations │ │ rule: "skip index dependents removal ops on relation drop" │ │ -│ └── • 4 Mutation operations +│ └── • 5 Mutation operations │ │ │ ├── • MarkDescriptorAsDropped │ │ DescriptorID: 107 │ │ +│ ├── • RemoveObjectParent +│ │ ObjectID: 107 +│ │ ParentSchemaID: 106 +│ │ │ ├── • RemoveTableComment │ │ TableID: 107 │ │ @@ -791,11 +795,15 @@ EXPLAIN (ddl, verbose) DROP TABLE db.sc.t; │ │ └── • skip PUBLIC → ABSENT operations │ │ rule: "skip index dependents removal ops on relation drop" │ │ -│ └── • 11 Mutation operations +│ └── • 12 Mutation operations │ │ │ ├── • MarkDescriptorAsDropped │ │ DescriptorID: 107 │ │ +│ ├── • RemoveObjectParent +│ │ ObjectID: 107 +│ │ ParentSchemaID: 106 +│ │ │ ├── • RemoveTableComment │ │ TableID: 107 │ │