Skip to content

Commit

Permalink
new commit1
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangziheng01233 committed Jul 4, 2023
1 parent 7bcc0ff commit a3db9ab
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 13 deletions.
26 changes: 18 additions & 8 deletions src/sdk/sql_cluster_router.cc
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ bool SQLClusterRouter::DropDB(const std::string& db, hybridse::sdk::Status* stat
return true;
}

bool SQLClusterRouter::DropTable(const std::string& db, const std::string& table, hybridse::sdk::Status* status) {
bool SQLClusterRouter::DropTable(const std::string& db, const std::string& table, const bool if_not_exists, hybridse::sdk::Status* status) {
RET_FALSE_IF_NULL_AND_WARN(status, "output status is nullptr");
if (db.empty() || table.empty()) {
SET_STATUS_AND_WARN(status, StatusCode::kCmdError,
Expand All @@ -916,20 +916,30 @@ bool SQLClusterRouter::DropTable(const std::string& db, const std::string& table
return false;
}

auto tableInfo = GetTableInfo(db, table);
auto table_info = cluster_sdk_ -> GetTableInfo(db, table);

if (table_info == nullptr) {
if (if_not_exists) {
*status= {};
return true;
} else {
SET_STATUS_AND_WARN(status, StatusCode::kCmdError, "table not exists");
return false;
}
}

// delete pre-aggr meta info if need
if (tableInfo.base_table_tid() > 0) {
if (table_info->base_table_tid() > 0) {
std::string meta_db = openmldb::nameserver::INTERNAL_DB;
std::string meta_table = openmldb::nameserver::PRE_AGG_META_NAME;
std::string select_aggr_info =
absl::StrCat("select base_db,base_table,aggr_func,aggr_col,partition_cols,order_by_col,filter_col from ",
meta_db, ".", meta_table, " where aggr_table = '", tableInfo.name(), "';");
meta_db, ".", meta_table, " where aggr_table = '", table_info->name(), "';");
auto rs = ExecuteSQL("", select_aggr_info, true, true, 0, status);
WARN_NOT_OK_AND_RET(status, "get aggr info failed", false);
if (rs->Size() != 1) {
SET_STATUS_AND_WARN(status, StatusCode::kCmdError,
"duplicate records generate with aggr table name: " + tableInfo.name());
"duplicate records generate with aggr table name: " + table_info->name());
return false;
}
std::string idx_key;
Expand Down Expand Up @@ -961,15 +971,15 @@ bool SQLClusterRouter::DropTable(const std::string& db, const std::string& table
}
auto tid = cluster_sdk_->GetTableId(meta_db, meta_table);
std::string msg;
if (!tablet_client->Delete(tid, 0, tableInfo.name(), "aggr_table", msg) ||
if (!tablet_client->Delete(tid, 0, table_info->name(), "aggr_table", msg) ||
!tablet_client->Delete(tid, 0, idx_key, "unique_key", msg)) {
SET_STATUS_AND_WARN(status, StatusCode::kCmdError, "delete aggr meta failed");
return false;
}
}

// Check offline table info first
if (tableInfo.has_offline_table_info()) {
if (table_info->has_offline_table_info()) {
auto taskmanager_client_ptr = cluster_sdk_->GetTaskManagerClient();
if (!taskmanager_client_ptr) {
SET_STATUS_AND_WARN(status, StatusCode::kRuntimeError, "no taskmanager client");
Expand Down Expand Up @@ -1886,7 +1896,7 @@ std::shared_ptr<hybridse::sdk::ResultSet> SQLClusterRouter::HandleSQLCmd(const h
if (!CheckAnswerIfInteractive("table", table_name)) {
return {};
}
if (DropTable(db_name, table_name, status)) {
if (DropTable(db_name, table_name, cmd_node -> IsIfNotExists(), status)) {
RefreshCatalog();
}
return {};
Expand Down
2 changes: 1 addition & 1 deletion src/sdk/sql_cluster_router.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class SQLClusterRouter : public SQLRouter {

bool DropDB(const std::string& db, hybridse::sdk::Status* status) override;

bool DropTable(const std::string& db, const std::string& table, hybridse::sdk::Status* status);
bool DropTable(const std::string& db, const std::string& table, const bool if_not_exists, hybridse::sdk::Status* status);

bool ShowDB(std::vector<std::string>* dbs, hybridse::sdk::Status* status) override;

Expand Down
47 changes: 43 additions & 4 deletions src/sdk/sql_cluster_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,66 @@ class SQLClusterDDLTest : public SQLClusterTest {
std::shared_ptr<SQLRouter> router;
std::string db;
};
TEST_F(SQLClusterDDLTest, DropTable) {
std::string name = "test" + GenRand();
::hybridse::sdk::Status status;
std::string ddl;

std::string db2 = "db" + GenRand();
ASSERT_TRUE(router->CreateDB(db2, &status));
// drop table name
ddl = "create table " + db2 + "." + name +
"("
"col1 int, col2 bigint, col3 string,"
"index(key=col3, ts=col2));";
ASSERT_TRUE(router->ExecuteDDL(db, ddl, &status));
ASSERT_TRUE(router->ExecuteDDL(db, "drop table " + db2 + "." + name + ";", &status));

//drop table name when name not exist
ASSERT_FALSE(router->ExecuteDDL(db, "drop table " + db2 + "." + name + ";", &status));

ASSERT_TRUE(router->DropDB(db2, &status));
}
TEST_F(SQLClusterDDLTest, DropTableIfExists) {
std::string name = "test" + GenRand();
::hybridse::sdk::Status status;
std::string ddl;

std::string db2 = "db" + GenRand();
ASSERT_TRUE(router->CreateDB(db2, &status));
// drop table name
ddl = "create table " + db2 + "." + name +
"("
"col1 int, col2 bigint, col3 string,"
"index(key=col3, ts=col2));";
ASSERT_TRUE(router->ExecuteDDL(db, ddl, &status));
ASSERT_TRUE(router->ExecuteDDL(db, "drop table if exists " + db2 + "." + name + ";", &status));

//drop table name when name not exist
ASSERT_TRUE(router->ExecuteDDL(db, "drop table if exists " + db2 + "." + name + ";", &status));

ASSERT_TRUE(router->DropDB(db2, &status));
}

TEST_F(SQLClusterDDLTest, DropDatabase) {
std::string db2 = "db" + GenRand();
::hybridse::sdk::Status status;
std::string ddl;

ASSERT_TRUE(router->CreateDB(db2, &status));
// drop database db2

// drop database db2
ASSERT_TRUE(router->ExecuteDDL(db, "drop database " + db2 + ";", &status));

//drop database db2 when db2 not exist
ASSERT_FALSE(router->ExecuteDDL(db, "drop database db2;", &status));
}
TEST_F(SQLClusterDDLTest, DropDatabaseIfNotExist) {
TEST_F(SQLClusterDDLTest, DropDatabaseIfExists) {
std::string db2 = "db" + GenRand();
::hybridse::sdk::Status status;
std::string ddl;

ASSERT_TRUE(router->CreateDB(db2, &status));
// drop database db2

ASSERT_TRUE(router->ExecuteDDL(db, "drop database if exists " + db2 + ";", &status));

//drop database db2 when db2 not exist
Expand Down

0 comments on commit a3db9ab

Please sign in to comment.