Skip to content

Commit 413d0e9

Browse files
committed
fix review comments
1 parent db62449 commit 413d0e9

File tree

6 files changed

+48
-50
lines changed

6 files changed

+48
-50
lines changed

src/meta/api/src/api_impl/ref_api.rs

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
use std::collections::HashMap;
1616
use std::collections::HashSet;
17-
use std::sync::Arc;
1817

1918
use chrono::DateTime;
2019
use chrono::Utc;
@@ -25,7 +24,6 @@ use databend_common_meta_app::app_error::TableSnapshotExpired;
2524
use databend_common_meta_app::app_error::TableVersionMismatched;
2625
use databend_common_meta_app::app_error::UndropTableRetentionGuard;
2726
use databend_common_meta_app::app_error::UnknownReference;
28-
use databend_common_meta_app::app_error::UnknownTable;
2927
use databend_common_meta_app::app_error::UnknownTableId;
3028
use databend_common_meta_app::data_mask::MaskPolicyIdTableId;
3129
use databend_common_meta_app::data_mask::MaskPolicyTableId;
@@ -41,7 +39,6 @@ use databend_common_meta_app::schema::AutoIncrementStorageValue;
4139
use databend_common_meta_app::schema::BranchIdToName;
4240
use databend_common_meta_app::schema::CreateTableBranchReq;
4341
use databend_common_meta_app::schema::CreateTableTagReq;
44-
use databend_common_meta_app::schema::DBIdTableName;
4542
use databend_common_meta_app::schema::DatabaseType;
4643
use databend_common_meta_app::schema::DropTableBranchReq;
4744
use databend_common_meta_app::schema::DropTableTagReq;
@@ -85,9 +82,7 @@ use log::debug;
8582
use log::warn;
8683

8784
use crate::api_impl::schema_api::restore_policy_references_on_undrop;
88-
use crate::database_util::get_db_or_err;
8985
use crate::fetch_id;
90-
use crate::get_u64_value;
9186
use crate::kv_app_error::KVAppError;
9287
use crate::kv_pb_api::KVPbApi;
9388
use 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.

src/meta/api/src/api_impl/table_api.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,6 +1734,26 @@ where
17341734
Ok(table_name)
17351735
}
17361736

1737+
#[logcall::logcall]
1738+
#[fastrace::trace]
1739+
async fn resolve_table_id(&self, name_ident: &TableNameIdent) -> Result<u64, KVAppError> {
1740+
let tenant_dbname = name_ident.db_name_ident();
1741+
let (seq_db_id, _db_meta) = get_db_or_err(
1742+
self,
1743+
&tenant_dbname,
1744+
format!("resolve_table_id: {}", tenant_dbname.display()),
1745+
)
1746+
.await?;
1747+
let dbid_tbname = DBIdTableName::new(*seq_db_id.data, name_ident.table_name.clone());
1748+
let (tb_id_seq, table_id) = get_u64_value(self, &dbid_tbname).await?;
1749+
if tb_id_seq == 0 {
1750+
return Err(KVAppError::AppError(AppError::UnknownTable(
1751+
UnknownTable::new(&name_ident.table_name, "resolve_table_id"),
1752+
)));
1753+
}
1754+
Ok(table_id)
1755+
}
1756+
17371757
#[logcall::logcall]
17381758
#[fastrace::trace]
17391759
async fn get_table_copied_file_info(

src/meta/app/src/schema/table/refs.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use databend_meta_client::types::SeqV;
2424
use super::TableId;
2525
use super::TableLvtCheck;
2626
use super::TableMeta;
27-
use super::TableNameIdent;
2827
use crate::tenant::Tenant;
2928

3029
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -145,7 +144,7 @@ pub struct CreateTableBranchReq {
145144
/// Base table id that owns the branch namespace.
146145
pub base_table_id: u64,
147146
/// Fork from this branch. `None` means fork from the base table itself.
148-
pub from_branch_id: Option<u64>,
147+
pub source_branch_id: Option<u64>,
149148
pub branch_name: String,
150149
/// Optimistic seq check for the fork source.
151150
pub seq: MatchSeq,
@@ -165,7 +164,7 @@ pub struct DropTableBranchReq {
165164

166165
#[derive(Clone, Debug, PartialEq, Eq)]
167166
pub struct GetTableBranchReq {
168-
pub name_ident: TableNameIdent,
167+
pub table_id: u64,
169168
pub branch_name: String,
170169
pub include_expired: bool,
171170
}

src/meta/schema-api-test-suite/src/schema_api_test_suite.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7579,7 +7579,7 @@ impl SchemaApiTestSuite {
75797579
.create_table_branch(CreateTableBranchReq {
75807580
tenant: tenant.clone(),
75817581
base_table_id: table_info.ident.table_id,
7582-
from_branch_id: None,
7582+
source_branch_id: None,
75837583
seq: MatchSeq::Exact(table_info.ident.seq),
75847584
branch_name: branch_name.to_string(),
75857585
new_table_meta: table_info.meta.clone(),

src/query/ee/src/table_ref/handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl TableRefHandler for RealTableRefHandler {
183183
.create_table_branch(CreateTableBranchReq {
184184
tenant: ctx.get_tenant(),
185185
base_table_id,
186-
from_branch_id: (source_table_id != base_table_id).then_some(source_table_id),
186+
source_branch_id: (source_table_id != base_table_id).then_some(source_table_id),
187187
branch_name: plan.branch_name.clone(),
188188
seq: MatchSeq::Exact(seq),
189189
new_table_meta: branch_table_meta,

src/query/service/src/catalogs/default/mutable_catalog.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,13 +671,18 @@ impl Catalog for MutableCatalog {
671671
branch_name: &str,
672672
include_expired: bool,
673673
) -> Result<Arc<dyn Table>> {
674+
let name_ident = TableNameIdent::new(tenant, db_name, table_name);
675+
let table_id = self.ctx.meta.resolve_table_id(&name_ident).await?;
674676
let req = GetTableBranchReq {
675-
name_ident: TableNameIdent::new(tenant, db_name, table_name),
677+
table_id,
676678
branch_name: branch_name.to_string(),
677679
include_expired,
678680
};
679681
let info = self.ctx.meta.get_table_branch(req).await?;
680-
self.get_table_by_info(info.as_ref())
682+
let mut info = info;
683+
info.name = table_name.to_string();
684+
info.desc = format!("'{}'.'{}'/'{}'", db_name, table_name, branch_name);
685+
self.get_table_by_info(&info)
681686
}
682687

683688
#[async_backtrace::framed]

0 commit comments

Comments
 (0)