Skip to content

Commit

Permalink
fix create index if not exists behavior (#4705)
Browse files Browse the repository at this point in the history
Co-authored-by: Sophie <84560950+Sophie-Xie@users.noreply.github.com>
Co-authored-by: Doodle <13706157+critical27@users.noreply.github.com>
  • Loading branch information
3 people committed Oct 13, 2022
1 parent 0e713eb commit 90218d7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/meta/processors/index/CreateEdgeIndexProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ void CreateEdgeIndexProcessor::process(const cpp2::CreateEdgeIndexReq& req) {
const auto& indexName = req.get_index_name();
auto& edgeName = req.get_edge_name();
const auto& fields = req.get_fields();
auto ifNotExists = req.get_if_not_exists();

std::set<std::string> columnSet;
for (const auto& field : fields) {
Expand All @@ -41,7 +42,7 @@ void CreateEdgeIndexProcessor::process(const cpp2::CreateEdgeIndexReq& req) {
// check if the space already exist index has the same index name
auto ret = getIndexID(space, indexName);
if (nebula::ok(ret)) {
if (req.get_if_not_exists()) {
if (ifNotExists) {
handleErrorCode(nebula::cpp2::ErrorCode::SUCCEEDED);
} else {
LOG(INFO) << "Create Edge Index Failed: " << indexName << " has existed";
Expand Down Expand Up @@ -96,7 +97,15 @@ void CreateEdgeIndexProcessor::process(const cpp2::CreateEdgeIndexReq& req) {
}

if (checkIndexExist(fields, item)) {
resp_.code_ref() = nebula::cpp2::ErrorCode::E_EXISTED;
if (ifNotExists) {
resp_.code_ref() = nebula::cpp2::ErrorCode::SUCCEEDED;
cpp2::ID thriftID;
// Fill index id to avoid broken promise
thriftID.index_id_ref() = item.get_index_id();
resp_.id_ref() = thriftID;
} else {
resp_.code_ref() = nebula::cpp2::ErrorCode::E_EXISTED;
}
onFinished();
return;
}
Expand Down
13 changes: 11 additions & 2 deletions src/meta/processors/index/CreateTagIndexProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ void CreateTagIndexProcessor::process(const cpp2::CreateTagIndexReq& req) {
const auto& indexName = req.get_index_name();
auto& tagName = req.get_tag_name();
const auto& fields = req.get_fields();
auto ifNotExists = req.get_if_not_exists();

std::set<std::string> columnSet;
for (const auto& field : fields) {
Expand All @@ -42,7 +43,7 @@ void CreateTagIndexProcessor::process(const cpp2::CreateTagIndexReq& req) {
// check if the space has the index with the same name
auto ret = getIndexID(space, indexName);
if (nebula::ok(ret)) {
if (req.get_if_not_exists()) {
if (ifNotExists) {
handleErrorCode(nebula::cpp2::ErrorCode::SUCCEEDED);
} else {
LOG(INFO) << "Create Tag Index Failed: " << indexName << " has existed";
Expand Down Expand Up @@ -96,7 +97,15 @@ void CreateTagIndexProcessor::process(const cpp2::CreateTagIndexReq& req) {
}

if (checkIndexExist(fields, item)) {
resp_.code_ref() = nebula::cpp2::ErrorCode::E_EXISTED;
if (ifNotExists) {
resp_.code_ref() = nebula::cpp2::ErrorCode::SUCCEEDED;
cpp2::ID thriftID;
// Fill index id to avoid broken promise
thriftID.index_id_ref() = item.get_index_id();
resp_.id_ref() = thriftID;
} else {
resp_.code_ref() = nebula::cpp2::ErrorCode::E_EXISTED;
}
onFinished();
return;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/tck/features/index/Index.feature
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ Feature: IndexTest_Vid_String
CREATE TAG INDEX single_tag_index ON tag_1(col2);
"""
Then the execution should be successful
When executing query:
"""
CREATE TAG INDEX IF NOT EXISTS single_tag_index_1 ON tag_1(col2);
"""
Then the execution should be successful
When executing query:
"""
CREATE TAG INDEX duplicate_tag_index_1 ON tag_1(col2);
Expand Down Expand Up @@ -155,6 +160,11 @@ Feature: IndexTest_Vid_String
CREATE EDGE INDEX single_edge_index ON edge_1(col2);
"""
Then the execution should be successful
When executing query:
"""
CREATE EDGE INDEX IF NOT EXISTS single_edge_index_1 ON edge_1(col2);
"""
Then the execution should be successful
When executing query:
"""
CREATE EDGE INDEX duplicate_edge_1_index ON edge_1(col2)
Expand Down

0 comments on commit 90218d7

Please sign in to comment.