Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: unsupport create table like when database is not found #3379

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/sdk/db_sdk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,15 @@ bool ClusterSDK::BuildCatalog() {
return UpdateCatalog(table_datas, sp_datas);
}

std::vector<std::string> DBSDK::GetAllDbs() {
std::lock_guard<::openmldb::base::SpinMutex> lock(mu_);
std::vector<std::string> all_dbs;
dl239 marked this conversation as resolved.
Show resolved Hide resolved
for (auto db_name_iter = table_to_tablets_.begin(); db_name_iter != table_to_tablets_.end(); db_name_iter++) {
all_dbs.push_back(db_name_iter->first);
}
return all_dbs;
}

uint32_t DBSDK::GetTableId(const std::string& db, const std::string& tname) {
auto table_handler = GetCatalog()->GetTable(db, tname);
auto* sdk_table_handler = dynamic_cast<::openmldb::catalog::SDKTableHandler*>(table_handler.get());
Expand Down
1 change: 1 addition & 0 deletions src/sdk/db_sdk.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class DBSDK {

std::shared_ptr<::openmldb::client::TaskManagerClient> GetTaskManagerClient();

std::vector<std::string> GetAllDbs();
uint32_t GetTableId(const std::string& db, const std::string& tname);
std::shared_ptr<::openmldb::nameserver::TableInfo> GetTableInfo(const std::string& db, const std::string& tname);
std::vector<std::shared_ptr<::openmldb::nameserver::TableInfo>> GetTables(const std::string& db);
Expand Down
15 changes: 11 additions & 4 deletions src/sdk/sql_cluster_router.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1930,11 +1930,12 @@ base::Status SQLClusterRouter::HandleSQLCreateTable(hybridse::node::CreatePlanNo
return base::Status(base::ReturnCode::kSQLCmdRunError, "fail to execute plan : null pointer");
}

if (create_node->like_clause_ == nullptr) {
std::string db_name = create_node->GetDatabase().empty() ? db : create_node->GetDatabase();
if (db_name.empty()) {
return base::Status(base::ReturnCode::kSQLCmdRunError, "ERROR: Please use database first");
std::string db_name = create_node->GetDatabase().empty() ? db : create_node->GetDatabase();
if (db_name.empty()) {
return base::Status(base::ReturnCode::kSQLCmdRunError, "ERROR: Please use database first");
}

if (create_node->like_clause_ == nullptr) {
::openmldb::nameserver::TableInfo table_info;
table_info.set_db(db_name);

Expand All @@ -1955,6 +1956,12 @@ base::Status SQLClusterRouter::HandleSQLCreateTable(hybridse::node::CreatePlanNo
return base::Status(base::ReturnCode::kSQLCmdRunError, msg);
}
} else {
auto dbs = cluster_sdk_->GetAllDbs();
auto it = std::find(dbs.begin(), dbs.end(), db_name);
if (it == dbs.end()) {
return base::Status(base::ReturnCode::kSQLCmdRunError, "fail to create, database does not exist!");
}

LOG(WARNING) << "CREATE TABLE LIKE will run in offline job, please wait.";

std::map<std::string, std::string> config;
Expand Down
7 changes: 7 additions & 0 deletions src/sdk/sql_cluster_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ class SQLClusterDDLTest : public SQLClusterTest {
std::string db;
};

TEST_F(SQLClusterDDLTest, TestCreateTableLike) {
::hybridse::sdk::Status status;

ASSERT_FALSE(router->ExecuteDDL(db, "create table db2.tb like hive 'hive://db.tb';", &status));
ASSERT_FALSE(router->ExecuteDDL(db, "drop table db2.tb;", &status));
}

TEST_F(SQLClusterDDLTest, TestIfExists) {
std::string name = "test" + GenRand();
::hybridse::sdk::Status status;
Expand Down