@@ -32,43 +32,16 @@ func RenameTable(b BuildCtx, n *tree.RenameTable) {
3232 }
3333
3434 // Validate the object type matches what was requested.
35- validateObjectType (elts , n )
35+ validateObjectType (elts , n . Name . ToTableName (). ObjectName , n . IsSequence , n . IsView , n . IsMaterialized )
3636
3737 // Get the fully qualified object name.
38- objectName := n .Name .ToTableName ()
39- dbElts , scElts := b .ResolveTargetObject (n .Name , privilege .CREATE /* this should be 0 */ )
40- _ , _ , scName := scpb .FindNamespace (scElts )
41- _ , _ , dbname := scpb .FindNamespace (dbElts )
42- objectName .SchemaName = tree .Name (scName .Name )
43- objectName .CatalogName = tree .Name (dbname .Name )
44- objectName .ExplicitCatalog = true
45- objectName .ExplicitSchema = true
38+ objectName := getAlterTableQualifiedObjectName (n .Name , b )
4639
4740 // Get the descriptor ID for further processing.
48- var targetDescriptorID catid.DescID
49- var targetElement scpb.Element
50- _ , _ , tbl := scpb .FindTable (elts )
51- if tbl != nil {
52- targetElement = tbl
53- targetDescriptorID = tbl .TableID
54- }
55- if n .IsView || targetDescriptorID == 0 {
56- _ , _ , view := scpb .FindView (elts )
57- if view != nil {
58- targetElement = view
59- targetDescriptorID = view .ViewID
60- }
61- }
62- if n .IsSequence || targetDescriptorID == 0 {
63- _ , _ , seq := scpb .FindSequence (elts )
64- if seq != nil {
65- targetElement = seq
66- targetDescriptorID = seq .SequenceID
67- }
68- }
41+ targetDescriptorID , targetElement , _ := getAlterTableTargetElement (elts )
6942
7043 // Check for name-based dependencies that would prevent renaming.
71- checkNameBasedDependencies (b , targetDescriptorID , targetElement , objectName )
44+ checkNameBasedDependencies (b , targetDescriptorID , targetElement , objectName , "rename" )
7245
7346 // Need CREATE privilege on database to match legacy schema changer behavior.
7447 b .ResolveDatabase (objectName .CatalogName , ResolveParams {RequiredPrivilege : privilege .CREATE })
@@ -104,6 +77,7 @@ func RenameTable(b BuildCtx, n *tree.RenameTable) {
10477 newName .ExplicitCatalog = true
10578 newName .ExplicitSchema = true
10679 checkTableNameConflicts (b , objectName , newName , currentNS )
80+ validateTableRename (b , objectName , newName )
10781
10882 // Check if the new name is the same as the old name (no-op case).
10983 if currentNS .Name == string (newName .ObjectName ) {
@@ -129,6 +103,18 @@ func RenameTable(b BuildCtx, n *tree.RenameTable) {
129103 b .LogEventForExistingPayload (newNS , renameEvent )
130104}
131105
106+ func getAlterTableQualifiedObjectName (name * tree.UnresolvedObjectName , b BuildCtx ) tree.TableName {
107+ objectName := name .ToTableName ()
108+ dbElts , scElts := b .ResolveTargetObject (name , privilege .CREATE /* this should be 0 */ )
109+ _ , _ , scName := scpb .FindNamespace (scElts )
110+ _ , _ , dbname := scpb .FindNamespace (dbElts )
111+ objectName .SchemaName = tree .Name (scName .Name )
112+ objectName .CatalogName = tree .Name (dbname .Name )
113+ objectName .ExplicitCatalog = true
114+ objectName .ExplicitSchema = true
115+ return objectName
116+ }
117+
132118// validateTableRename performs validation checks before renaming a table.
133119func validateTableRename (b BuildCtx , currentName tree.TableName , newName tree.TableName ) {
134120 // The legacy schema changer used to check the CREATE privilege on the
@@ -187,34 +173,51 @@ func checkTableNameConflicts(
187173 }
188174 panic (sqlerrors .NewRelationAlreadyExistsError (newName .String ()))
189175 }
176+ }
190177
191- validateTableRename (b , currentName , newName )
178+ func getAlterTableTargetElement (
179+ elts ElementResultSet ,
180+ ) (descID catid.DescID , element scpb.Element , isTemp bool ) {
181+ if tbl := elts .FilterTable ().MustGetZeroOrOneElement (); tbl != nil {
182+ element = tbl
183+ descID = tbl .TableID
184+ isTemp = tbl .IsTemporary
185+ } else if view := elts .FilterView ().MustGetZeroOrOneElement (); view != nil {
186+ element = view
187+ descID = view .ViewID
188+ isTemp = view .IsTemporary
189+ } else if seq := elts .FilterSequence ().MustGetZeroOrOneElement (); seq != nil {
190+ element = seq
191+ descID = seq .SequenceID
192+ isTemp = seq .IsTemporary
193+ }
194+ return descID , element , isTemp
192195}
193196
194197// validateObjectType validates that the resolved object type matches what was
195198// requested in the statement. Note that we allow ALTER TABLE to be used for
196199// views or sequences, just like in Postgres.
197- func validateObjectType (elts ElementResultSet , n * tree.RenameTable ) {
200+ func validateObjectType (
201+ elts ElementResultSet , objectName tree.Name , isSequence bool , isView bool , isMaterialized bool ,
202+ ) {
198203 _ , _ , view := scpb .FindView (elts )
199204 _ , _ , seq := scpb .FindSequence (elts )
200205
201- objectName := n .Name .ToTableName ().ObjectName
202-
203- if n .IsView && view == nil {
206+ if isView && view == nil {
204207 // User asked for view but we found something else.
205208 panic (pgerror .Newf (pgcode .WrongObjectType , "%q is not a view" , objectName ))
206- } else if n . IsSequence && seq == nil {
209+ } else if isSequence && seq == nil {
207210 // User asked for sequence but we found something else.
208211 panic (pgerror .Newf (pgcode .WrongObjectType , "%q is not a sequence" , objectName ))
209212 }
210213
211214 if view != nil {
212215 // Validate view type (materialized vs non-materialized).
213- if view .IsMaterialized && ! n . IsMaterialized {
216+ if view .IsMaterialized && ! isMaterialized {
214217 panic (errors .WithHint (pgerror .Newf (pgcode .WrongObjectType , "%q is a materialized view" , objectName ),
215218 "use the corresponding MATERIALIZED VIEW command" ))
216219 }
217- if ! view .IsMaterialized && n . IsMaterialized {
220+ if ! view .IsMaterialized && isMaterialized {
218221 panic (pgerror .Newf (pgcode .WrongObjectType , "%q is not a materialized view" , objectName ))
219222 }
220223 }
@@ -223,7 +226,7 @@ func validateObjectType(elts ElementResultSet, n *tree.RenameTable) {
223226// checkNameBasedDependencies validates that no objects depend on this object
224227// via its name.
225228func checkNameBasedDependencies (
226- b BuildCtx , descriptorID catid.DescID , element scpb.Element , objectName tree.TableName ,
229+ b BuildCtx , descriptorID catid.DescID , element scpb.Element , objectName tree.TableName , op string ,
227230) {
228231 switch element .(type ) {
229232 case * scpb.Sequence :
@@ -238,14 +241,14 @@ func checkNameBasedDependencies(
238241 // blocked.
239242 viewElts := b .QueryByID (backRefElem .ViewID )
240243 _ , _ , viewNS := scpb .FindNamespace (viewElts )
241- panic (sqlerrors .NewDependentBlocksOpError ("rename" , "relation" , objectName .String (), "view" , viewNS .Name ))
244+ panic (sqlerrors .NewDependentBlocksOpError (op , "relation" , objectName .String (), "view" , viewNS .Name ))
242245 case * scpb.FunctionName :
243246 funcElem := b .QueryByID (backRefElem .FunctionID ).FilterFunction ().MustGetOneElement ()
244247 funcType := "function"
245248 if funcElem .IsProcedure {
246249 funcType = "procedure"
247250 }
248- panic (sqlerrors .NewDependentBlocksOpError ("rename" , "relation" , objectName .String (), funcType , backRefElem .Name ))
251+ panic (sqlerrors .NewDependentBlocksOpError (op , "relation" , objectName .String (), funcType , backRefElem .Name ))
249252 case * scpb.TriggerDeps :
250253 for _ , usesRelation := range backRefElem .UsesRelations {
251254 if usesRelation .ID == descriptorID {
@@ -255,9 +258,8 @@ func checkNameBasedDependencies(
255258 dependentTriggerName := backRefs .FilterTriggerName ().Filter (func (_ scpb.Status , _ scpb.TargetStatus , e * scpb.TriggerName ) bool {
256259 return e .TriggerID == dependentTriggerID && e .TableID == dependentTableID
257260 }).MustGetOneElement ()
258- panic (sqlerrors .NewDependentObjectErrorf (
259- "cannot rename relation %q because trigger %q on table %q depends on it" ,
260- objectName .String (), dependentTriggerName .Name , dependentTableNS .Name ,
261+ panic (sqlerrors .NewDependentBlocksOpError (
262+ op , "relation" , objectName .String (), dependentTriggerName .Name , dependentTableNS .Name ,
261263 ))
262264 }
263265 }
0 commit comments