Skip to content

Commit e6efbff

Browse files
committed
fix review comments
1 parent e1d18f0 commit e6efbff

2 files changed

Lines changed: 36 additions & 44 deletions

File tree

src/meta/api/src/api_impl/schema_api.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use chrono::DateTime;
2121
use chrono::Utc;
2222
use databend_common_meta_app::app_error::AppError;
2323
use databend_common_meta_app::app_error::DropTableWithDropTime;
24-
use databend_common_meta_app::app_error::TableEngineMismatch;
2524
use databend_common_meta_app::app_error::UndropTableAlreadyExists;
2625
use databend_common_meta_app::app_error::UndropTableHasNoHistory;
2726
use databend_common_meta_app::app_error::UndropTableRetentionGuard;
@@ -178,16 +177,22 @@ pub async fn construct_drop_table_txn_operations(
178177
tenant: &Tenant,
179178
catalog_name: Option<String>,
180179
table_id: u64,
181-
expected_engine: Option<&str>,
180+
preloaded_table_meta: Option<SeqV<TableMeta>>,
182181
db_id: u64,
183182
if_exists: bool,
184183
if_delete: bool,
185184
txn: &mut TxnRequest,
186185
) -> Result<(u64, u64), KVAppError> {
187186
let tbid = TableId { table_id };
188187

188+
// Reuse metadata already loaded by CREATE OR REPLACE; ordinary DROP callers
189+
// do not have it and load it here. A preloaded value must belong to `table_id`.
190+
let (tb_meta_seq, tb_meta) = if let Some(preloaded_table_meta) = preloaded_table_meta {
191+
(preloaded_table_meta.seq, Some(preloaded_table_meta.data))
192+
} else {
193+
kv_api.get_pb_seq_and_value(&tbid).await?
194+
};
189195
// Check if table exists.
190-
let (tb_meta_seq, tb_meta) = kv_api.get_pb_seq_and_value(&tbid).await?;
191196
if tb_meta_seq == 0 {
192197
return Err(KVAppError::AppError(AppError::UnknownTableId(
193198
UnknownTableId::new(table_id, "drop_table_by_id failed to find valid tb_meta"),
@@ -235,13 +240,6 @@ pub async fn construct_drop_table_txn_operations(
235240
);
236241

237242
let mut tb_meta = tb_meta.unwrap();
238-
if let Some(expected_engine) = expected_engine {
239-
if !tb_meta.engine.eq_ignore_ascii_case(expected_engine) {
240-
return Err(KVAppError::AppError(
241-
TableEngineMismatch::new(table_name, &tb_meta.engine, expected_engine).into(),
242-
));
243-
}
244-
}
245243
// drop a table with drop_on time
246244
if tb_meta.drop_on.is_some() {
247245
return if if_exists {

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

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -362,54 +362,48 @@ where
362362
});
363363
}
364364
CreateOption::CreateOrReplace => {
365-
if req.as_dropped {
366-
// CTAS does not call construct_drop_table_txn_operations(),
367-
// so validate its existing table here.
368-
let existing_meta =
369-
self.get_pb(&TableId::new(*id.data)).await?.ok_or_else(|| {
370-
KVAppError::AppError(AppError::UnknownTableId(
371-
UnknownTableId::new(
372-
*id.data,
373-
"create or replace failed to find existing table meta",
374-
),
375-
))
376-
})?;
377-
if !existing_meta
378-
.engine
379-
.eq_ignore_ascii_case(&req.table_meta.engine)
380-
{
381-
return Err(KVAppError::AppError(
382-
TableEngineMismatch::new(
383-
req.table_name(),
384-
&existing_meta.engine,
385-
&req.table_meta.engine,
386-
)
387-
.into(),
388-
));
389-
}
390-
391-
// Guard the engine check against a concurrent metadata update.
392-
txn.condition.push(txn_cond_seq(
393-
&TableId::new(*id.data),
394-
Eq,
395-
existing_meta.seq,
365+
// CREATE OR REPLACE must not replace an existing table with a
366+
// different engine.
367+
let existing_table_meta = self
368+
.get_pb(&TableId::new(*id.data))
369+
.await?
370+
.ok_or_else(|| {
371+
KVAppError::AppError(AppError::UnknownTableId(UnknownTableId::new(
372+
*id.data,
373+
"create or replace failed to find existing table meta",
374+
)))
375+
})?;
376+
if !existing_table_meta
377+
.engine
378+
.eq_ignore_ascii_case(&req.table_meta.engine)
379+
{
380+
return Err(KVAppError::AppError(
381+
TableEngineMismatch::new(
382+
req.table_name(),
383+
&existing_table_meta.engine,
384+
&req.table_meta.engine,
385+
)
386+
.into(),
396387
));
388+
}
389+
390+
if req.as_dropped {
397391
// If the table is being created as a dropped table, we do not
398392
// need to combine with drop_table_txn operations, just return
399393
// the sequence number associated with the value part of
400394
// the key-value pair (key_dbid_tbname, table_id).
401395

402396
SeqV::new(id.seq, *id.data)
403397
} else {
404-
// The drop helper validates the engine against the metadata
405-
// sequence used by the replacement transaction.
398+
// Reuse the metadata loaded for the engine check when marking the
399+
// existing table as dropped.
406400
let (seq, id) = construct_drop_table_txn_operations(
407401
self,
408402
req.name_ident.table_name.clone(),
409403
&req.name_ident.tenant,
410404
req.catalog_name.clone(),
411405
*id.data,
412-
Some(&req.table_meta.engine),
406+
Some(existing_table_meta),
413407
*seq_db_id.data,
414408
true,
415409
false,

0 commit comments

Comments
 (0)