diff --git a/src/catalog/catalog_set.cpp b/src/catalog/catalog_set.cpp index 0c36fd5a39..5938faa344 100644 --- a/src/catalog/catalog_set.cpp +++ b/src/catalog/catalog_set.cpp @@ -1,6 +1,5 @@ #include "catalog/catalog_set.h" -#include "common/assert.h" #include "common/exception/catalog.h" #include "common/string_format.h" @@ -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 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); @@ -82,5 +83,19 @@ std::unique_ptr 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 diff --git a/src/include/catalog/catalog_set.h b/src/include/catalog/catalog_set.h index 367dbbfa69..89b4dea9f7 100644 --- a/src/include/catalog/catalog_set.h +++ b/src/include/catalog/catalog_set.h @@ -25,6 +25,10 @@ class CatalogSet { static std::unique_ptr deserialize(common::Deserializer& deserializer); std::unique_ptr copy() const; +private: + void validateExist(const std::string& name) const; + void validateNotExist(const std::string& name) const; + private: common::case_insensitive_map_t> entries; }; diff --git a/test/test_files/ddl/ddl.test b/test/test_files/ddl/ddl.test index 92861218cd..43e43e146c 100644 --- a/test/test_files/ddl/ddl.test +++ b/test/test_files/ddl/ddl.test @@ -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))