1414
1515use std:: collections:: HashMap ;
1616use std:: collections:: HashSet ;
17- use std:: sync:: Arc ;
1817
1918use chrono:: DateTime ;
2019use chrono:: Utc ;
@@ -25,7 +24,6 @@ use databend_common_meta_app::app_error::TableSnapshotExpired;
2524use databend_common_meta_app:: app_error:: TableVersionMismatched ;
2625use databend_common_meta_app:: app_error:: UndropTableRetentionGuard ;
2726use databend_common_meta_app:: app_error:: UnknownReference ;
28- use databend_common_meta_app:: app_error:: UnknownTable ;
2927use databend_common_meta_app:: app_error:: UnknownTableId ;
3028use databend_common_meta_app:: data_mask:: MaskPolicyIdTableId ;
3129use databend_common_meta_app:: data_mask:: MaskPolicyTableId ;
@@ -41,7 +39,6 @@ use databend_common_meta_app::schema::AutoIncrementStorageValue;
4139use databend_common_meta_app:: schema:: BranchIdToName ;
4240use databend_common_meta_app:: schema:: CreateTableBranchReq ;
4341use databend_common_meta_app:: schema:: CreateTableTagReq ;
44- use databend_common_meta_app:: schema:: DBIdTableName ;
4542use databend_common_meta_app:: schema:: DatabaseType ;
4643use databend_common_meta_app:: schema:: DropTableBranchReq ;
4744use databend_common_meta_app:: schema:: DropTableTagReq ;
@@ -85,9 +82,7 @@ use log::debug;
8582use log:: warn;
8683
8784use crate :: api_impl:: schema_api:: restore_policy_references_on_undrop;
88- use crate :: database_util:: get_db_or_err;
8985use crate :: fetch_id;
90- use crate :: get_u64_value;
9186use crate :: kv_app_error:: KVAppError ;
9287use crate :: kv_pb_api:: KVPbApi ;
9388use crate :: txn_backoff:: txn_backoff;
@@ -310,13 +305,13 @@ where
310305 debug ! ( req : ? =( & req) ; "RefApi: {}" , func_name!( ) ) ;
311306
312307 let branch_name = & req. branch_name ;
313- let source_table_id = req. from_branch_id . unwrap_or ( req. base_table_id ) ;
308+ // `base_table_id` identifies the base table namespace where the new branch name lives.
309+ // `source_table_id` identifies the object being forked from: the base table itself, or
310+ // a source branch table when `source_branch_id` is set.
311+ let source_table_id = req. source_branch_id . unwrap_or ( req. base_table_id ) ;
314312 let key_source_table_id = TableId {
315313 table_id : source_table_id,
316314 } ;
317- let key_table_id = TableId {
318- table_id : req. base_table_id ,
319- } ;
320315 let key_branch = TableIdBranchName :: new ( req. base_table_id , branch_name) ;
321316
322317 let mut maybe_branch_id = None ;
@@ -351,9 +346,12 @@ where
351346 txn_cond_seq( & key_branch, Eq , 0 ) ,
352347 ] ;
353348
354- if req. from_branch_id . is_some ( ) {
349+ if req. source_branch_id . is_some ( ) {
355350 // The source may be a branch table, but the new branch is still created under the
356351 // base table namespace, so the base table must still exist and be active.
352+ let key_table_id = TableId {
353+ table_id : req. base_table_id ,
354+ } ;
357355 let Some ( seq_table_meta) = self . get_pb ( & key_table_id) . await ? else {
358356 return Err ( KVAppError :: AppError ( AppError :: UnknownTableId (
359357 UnknownTableId :: new ( req. base_table_id , "create_table_branch: base table" ) ,
@@ -594,36 +592,11 @@ where
594592 /// Resolve a visible branch name to its current branch table metadata.
595593 #[ logcall:: logcall]
596594 #[ fastrace:: trace]
597- async fn get_table_branch ( & self , req : GetTableBranchReq ) -> Result < Arc < TableInfo > , KVAppError > {
595+ async fn get_table_branch ( & self , req : GetTableBranchReq ) -> Result < TableInfo , KVAppError > {
598596 debug ! ( req : ? =( & req) ; "RefApi: {}" , func_name!( ) ) ;
599597
600- let tenant_dbname = req. name_ident . db_name_ident ( ) ;
601- let ( seq_db_id, _db_meta) = get_db_or_err (
602- self ,
603- & tenant_dbname,
604- format ! ( "{}: {}" , "get_table_branch" , tenant_dbname. display( ) ) ,
605- )
606- . await ?;
607- let db_id = * seq_db_id. data ;
608-
609- let dbid_tbname = DBIdTableName {
610- db_id,
611- table_name : req. name_ident . table_name . clone ( ) ,
612- } ;
613- // Branch names are resolved under the *currently visible* base table id of `db.table`.
614- // This is intentional: branches are treated as refs of the current base table namespace,
615- // not as independently addressable objects. If the base table is dropped or replaced and
616- // `db.table` points to a new table id, previously created branches under the old base
617- // table id become unreachable by design.
618- let ( tb_id_seq, table_id) = get_u64_value ( self , & dbid_tbname) . await ?;
619- if tb_id_seq == 0 {
620- return Err ( KVAppError :: AppError ( AppError :: UnknownTable (
621- UnknownTable :: new ( & req. name_ident . table_name , "get_table_branch" ) ,
622- ) ) ) ;
623- }
624-
625598 let branch_name = & req. branch_name ;
626- let key_branch = TableIdBranchName :: new ( table_id, branch_name) ;
599+ let key_branch = TableIdBranchName :: new ( req . table_id , branch_name) ;
627600 let seq_branch = self . get_pb ( & key_branch) . await ?;
628601 let Some ( seq_branch) = seq_branch else {
629602 return Err ( KVAppError :: AppError ( AppError :: from ( UnknownReference :: new (
@@ -658,19 +631,20 @@ where
658631 table_id : branch_id,
659632 seq : seq_table_meta. seq ,
660633 } ,
661- desc : format ! (
662- "'{}'.'{}'/'{}'" ,
663- req. name_ident. db_name, req. name_ident. table_name, branch_name,
664- ) ,
665- name : req. name_ident . table_name . clone ( ) ,
634+ // Meta lookup only resolves the branch object by `table_id + branch_name`.
635+ // The caller rewrites this into the user-facing form `db.table/branch`.
636+ desc : branch_name. clone ( ) ,
637+ // `name` should be the base table name in the final TableInfo used by query code.
638+ // It is filled by the caller after resolving the branch under a specific table name.
639+ name : String :: new ( ) ,
666640 meta : seq_table_meta. data ,
667641 db_type : DatabaseType :: NormalDB ,
668642 // This meta API is currently reached only through the default catalog's branch lookup
669643 // path, so the returned TableInfo stays aligned with that default-catalog-only contract.
670644 catalog_info : Default :: default ( ) ,
671645 } ;
672646
673- Ok ( Arc :: new ( tb_info) )
647+ Ok ( tb_info)
674648 }
675649
676650 /// List table branches.
0 commit comments