Skip to content

Commit

Permalink
Fix issue 2265 (#3376)
Browse files Browse the repository at this point in the history
  • Loading branch information
andyfengHKU committed Apr 25, 2024
1 parent b491af2 commit 0dee918
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/catalog/catalog_set.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "catalog/catalog_set.h"

#include "common/assert.h"
#include "common/exception/catalog.h"
#include "common/string_format.h"

Expand All @@ -15,27 +14,29 @@ bool CatalogSet::containsEntry(const std::string& name) const {

CatalogEntry* CatalogSet::getEntry(const std::string& name) {
// LCOV_EXCL_START
// We should not trigger the following check. If so, we should throw more informative error
// message at catalog level.
if (!containsEntry(name)) {
throw CatalogException(stringFormat("Cannot find catalog entry with name {}.", name));
}
validateExist(name);
// LCOV_EXCL_STOP
return entries.at(name).get();
}

void CatalogSet::createEntry(std::unique_ptr<CatalogEntry> entry) {
KU_ASSERT(!entries.contains(entry->getName()));
// LCOV_EXCL_START
validateNotExist(entry->getName());
// LCOV_EXCL_STOP
entries.emplace(entry->getName(), std::move(entry));
}

void CatalogSet::removeEntry(const std::string& name) {
KU_ASSERT(containsEntry(name));
// LCOV_EXCL_START
validateExist(name);
// LCOV_EXCL_STOP
entries.erase(std::move(name));
}

void CatalogSet::renameEntry(const std::string& oldName, const std::string& newName) {
KU_ASSERT(containsEntry(oldName));
// LCOV_EXCL_START
validateExist(oldName);
// LCOV_EXCL_STOP
auto entry = std::move(entries.at(oldName));
entry->rename(newName);
entries.erase(oldName);
Expand Down Expand Up @@ -82,5 +83,19 @@ std::unique_ptr<CatalogSet> CatalogSet::copy() const {
return newCatalogSet;
}

// Ideally we should not trigger the following check. Instead, we should throw more informative
// error message at catalog level.
void CatalogSet::validateExist(const std::string& name) const {
if (!containsEntry(name)) {
throw CatalogException(stringFormat("{} does not exist in catalog.", name));
}
}

void CatalogSet::validateNotExist(const std::string& name) const {
if (containsEntry(name)) {
throw CatalogException(stringFormat("{} already exists in catalog.", name));
}
}

} // namespace catalog
} // namespace kuzu
4 changes: 4 additions & 0 deletions src/include/catalog/catalog_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class CatalogSet {
static std::unique_ptr<CatalogSet> deserialize(common::Deserializer& deserializer);
std::unique_ptr<CatalogSet> copy() const;

private:
void validateExist(const std::string& name) const;
void validateNotExist(const std::string& name) const;

private:
common::case_insensitive_map_t<std::unique_ptr<CatalogEntry>> entries;
};
Expand Down
14 changes: 14 additions & 0 deletions test/test_files/ddl/ddl.test
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ Table likes has been created.
-STATEMENT Call table_info('likes_person_organisation') RETURN *;
---- 1
1|since|INT64
-STATEMENT CREATE REL TABLE GROUP likes (FROM person TO person, FROM person TO organisation);
---- error
Binder exception: likes already exists in catalog.
-STATEMENT CREATE REL TABLE GROUP knows (FROM person TO person, FROM person TO organisation);
---- error
Binder exception: knows already exists in catalog.
-STATEMENT CREATE REL TABLE test_person_person (FROM person TO person);
---- ok
-STATEMENT CREATE REL TABLE GROUP test (FROM person TO person, FROM person TO organisation);
---- error
Catalog exception: test_person_person already exists in catalog.
-STATEMENT CREATE REL TABLE GROUP test2 (FROM person TO person, FROM person TO person);
---- error
Catalog exception: test2_person_person already exists in catalog.

-CASE CreateNodeTableCommitNormalExecution
-STATEMENT CREATE NODE TABLE EXAM_PAPER(STUDENT_ID INT64, MARK DOUBLE, PRIMARY KEY(STUDENT_ID))
Expand Down

0 comments on commit 0dee918

Please sign in to comment.