From 9bf077d24d95680f0cb553859b0e179de913deaf Mon Sep 17 00:00:00 2001 From: xiyang Date: Mon, 11 Dec 2023 02:18:46 +0800 Subject: [PATCH] rework table schema constructors --- CMakeLists.txt | 2 +- src/binder/bind/bind_copy.cpp | 4 +- src/binder/bind/bind_ddl.cpp | 5 +- src/binder/bind/ddl/bind_create_rdf_graph.cpp | 8 +-- src/catalog/catalog.cpp | 4 +- src/catalog/catalog_content.cpp | 8 +-- src/catalog/node_table_schema.cpp | 13 +++- src/catalog/rdf_graph_schema.cpp | 7 ++ src/catalog/rel_table_group_schema.cpp | 8 ++- src/catalog/rel_table_schema.cpp | 65 ++++++++++--------- src/catalog/table_schema.cpp | 17 ++++- .../binder/ddl/bound_create_table_info.h | 21 +++--- src/include/catalog/node_table_schema.h | 43 ++++-------- src/include/catalog/property.h | 6 +- src/include/catalog/rdf_graph_schema.h | 12 ++-- src/include/catalog/rel_table_group_schema.h | 9 +-- src/include/catalog/rel_table_schema.h | 49 ++++++-------- src/include/catalog/table_schema.h | 53 ++++++--------- src/include/storage/store/rel_table_data.h | 5 +- src/planner/plan/append_extend.cpp | 2 +- src/planner/plan/plan_copy.cpp | 10 ++- src/processor/map/map_copy_from.cpp | 5 +- src/processor/map/map_extend.cpp | 2 +- src/storage/stats/rel_table_statistics.cpp | 4 +- .../exceptions/catalog/catalog.test | 5 +- 25 files changed, 177 insertions(+), 190 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fe3b6074f9..245d6b386a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.15) -project(Kuzu VERSION 0.1.0.1 LANGUAGES CXX C) +project(Kuzu VERSION 0.1.0.2 LANGUAGES CXX C) find_package(Threads REQUIRED) diff --git a/src/binder/bind/bind_copy.cpp b/src/binder/bind/bind_copy.cpp index bdb06392fb..60dc1448bd 100644 --- a/src/binder/bind/bind_copy.cpp +++ b/src/binder/bind/bind_copy.cpp @@ -119,7 +119,7 @@ std::unique_ptr Binder::bindCopyNodeFrom(const Statement& statem auto& copyStatement = reinterpret_cast(statement); auto func = getScanFunction(config->fileType, *config); // For table with SERIAL columns, we need to read in serial from files. - auto containsSerial = tableSchema->containsColumnType(LogicalType(LogicalTypeID::SERIAL)); + auto containsSerial = tableSchema->containPropertyType(*LogicalType::SERIAL()); std::vector expectedColumnNames; std::vector> expectedColumnTypes; bindExpectedNodeColumns( @@ -146,7 +146,7 @@ std::unique_ptr Binder::bindCopyRelFrom(const parser::Statement& auto& copyStatement = reinterpret_cast(statement); auto func = getScanFunction(config->fileType, *config); // For table with SERIAL columns, we need to read in serial from files. - auto containsSerial = tableSchema->containsColumnType(LogicalType(LogicalTypeID::SERIAL)); + auto containsSerial = tableSchema->containPropertyType(*LogicalType::SERIAL()); KU_ASSERT(containsSerial == false); std::vector expectedColumnNames; std::vector> expectedColumnTypes; diff --git a/src/binder/bind/bind_ddl.cpp b/src/binder/bind/bind_ddl.cpp index 4b73bfb305..88322b6c55 100644 --- a/src/binder/bind/bind_ddl.cpp +++ b/src/binder/bind/bind_ddl.cpp @@ -118,13 +118,14 @@ std::unique_ptr Binder::bindCreateRelTableInfo(const Creat } } auto extraInfo = (ExtraCreateRelTableInfo*)info->extraInfo.get(); - auto relMultiplicity = getRelMultiplicityFromString(extraInfo->relMultiplicity); + auto srcMultiplicity = RelMultiplicityUtils::getFwd(extraInfo->relMultiplicity); + auto dstMultiplicity = RelMultiplicityUtils::getBwd(extraInfo->relMultiplicity); auto srcTableID = bindTableID(extraInfo->srcTableName); validateTableType(srcTableID, TableType::NODE); auto dstTableID = bindTableID(extraInfo->dstTableName); validateTableType(dstTableID, TableType::NODE); auto boundExtraInfo = std::make_unique( - relMultiplicity, srcTableID, dstTableID, std::move(boundProperties)); + srcMultiplicity, dstMultiplicity, srcTableID, dstTableID, std::move(boundProperties)); return std::make_unique( TableType::REL, info->tableName, std::move(boundExtraInfo)); } diff --git a/src/binder/bind/ddl/bind_create_rdf_graph.cpp b/src/binder/bind/ddl/bind_create_rdf_graph.cpp index 351a538fe0..dc60958e49 100644 --- a/src/binder/bind/ddl/bind_create_rdf_graph.cpp +++ b/src/binder/bind/ddl/bind_create_rdf_graph.cpp @@ -57,8 +57,8 @@ std::unique_ptr Binder::bindCreateRdfGraphInfo(const Creat resourceTripleProperties.push_back( std::make_unique(std::string(rdf::PID), LogicalType::INTERNAL_ID())); auto boundResourceTripleExtraInfo = - std::make_unique(RelMultiplicity::MANY_MANY, INVALID_TABLE_ID, - INVALID_TABLE_ID, std::move(resourceTripleProperties)); + std::make_unique(RelMultiplicity::MANY, RelMultiplicity::MANY, + INVALID_TABLE_ID, INVALID_TABLE_ID, std::move(resourceTripleProperties)); auto boundResourceTripleCreateInfo = std::make_unique( TableType::REL, resourceTripleTableName, std::move(boundResourceTripleExtraInfo)); // Literal triple table. @@ -67,8 +67,8 @@ std::unique_ptr Binder::bindCreateRdfGraphInfo(const Creat literalTripleProperties.push_back( std::make_unique(std::string(rdf::PID), LogicalType::INTERNAL_ID())); auto boundLiteralTripleExtraInfo = - std::make_unique(RelMultiplicity::MANY_MANY, INVALID_TABLE_ID, - INVALID_TABLE_ID, std::move(literalTripleProperties)); + std::make_unique(RelMultiplicity::MANY, RelMultiplicity::MANY, + INVALID_TABLE_ID, INVALID_TABLE_ID, std::move(literalTripleProperties)); auto boundLiteralTripleCreateInfo = std::make_unique( TableType::REL, literalTripleTableName, std::move(boundLiteralTripleExtraInfo)); // Rdf table. diff --git a/src/catalog/catalog.cpp b/src/catalog/catalog.cpp index 9f6d46c3a1..82ba399ba2 100644 --- a/src/catalog/catalog.cpp +++ b/src/catalog/catalog.cpp @@ -160,13 +160,13 @@ void Catalog::renameTable(table_id_t tableID, const std::string& newName) { void Catalog::addNodeProperty( table_id_t tableID, const std::string& propertyName, std::unique_ptr dataType) { KU_ASSERT(readWriteVersion != nullptr); - readWriteVersion->getTableSchema(tableID)->addNodeProperty(propertyName, std::move(dataType)); + readWriteVersion->getTableSchema(tableID)->addProperty(propertyName, std::move(dataType)); } void Catalog::addRelProperty( table_id_t tableID, const std::string& propertyName, std::unique_ptr dataType) { KU_ASSERT(readWriteVersion != nullptr); - readWriteVersion->getTableSchema(tableID)->addRelProperty(propertyName, std::move(dataType)); + readWriteVersion->getTableSchema(tableID)->addProperty(propertyName, std::move(dataType)); } void Catalog::dropProperty(table_id_t tableID, property_id_t propertyID) { diff --git a/src/catalog/catalog_content.cpp b/src/catalog/catalog_content.cpp index 4aa0941a4e..e19ffab254 100644 --- a/src/catalog/catalog_content.cpp +++ b/src/catalog/catalog_content.cpp @@ -71,9 +71,9 @@ table_id_t CatalogContent::addRelTableSchema(const BoundCreateTableInfo& info) { KU_ASSERT(srcNodeTableSchema && dstNodeTableSchema); srcNodeTableSchema->addFwdRelTableID(tableID); dstNodeTableSchema->addBwdRelTableID(tableID); - auto relTableSchema = - std::make_unique(info.tableName, tableID, extraInfo->relMultiplicity, - std::move(properties), extraInfo->srcTableID, extraInfo->dstTableID); + auto relTableSchema = std::make_unique(info.tableName, tableID, + std::move(properties), extraInfo->srcMultiplicity, extraInfo->dstMultiplicity, + extraInfo->srcTableID, extraInfo->dstTableID); tableNameToIDMap.emplace(relTableSchema->tableName, tableID); tableSchemas.emplace(tableID, std::move(relTableSchema)); return tableID; @@ -147,7 +147,7 @@ void CatalogContent::renameTable(table_id_t tableID, const std::string& newName) auto tableSchema = getTableSchema(tableID); tableNameToIDMap.erase(tableSchema->tableName); tableNameToIDMap.emplace(newName, tableID); - tableSchema->updateTableName(newName); + tableSchema->renameTable(newName); } static void validateStorageVersion(storage_version_t savedStorageVersion) { diff --git a/src/catalog/node_table_schema.cpp b/src/catalog/node_table_schema.cpp index 211e4b1fc9..2a76203f44 100644 --- a/src/catalog/node_table_schema.cpp +++ b/src/catalog/node_table_schema.cpp @@ -8,6 +8,12 @@ using namespace kuzu::common; namespace kuzu { namespace catalog { +NodeTableSchema::NodeTableSchema(const NodeTableSchema& other) : TableSchema{other} { + primaryKeyPropertyID = other.primaryKeyPropertyID; + fwdRelTableIDSet = other.fwdRelTableIDSet; + bwdRelTableIDSet = other.bwdRelTableIDSet; +} + void NodeTableSchema::serializeInternal(Serializer& serializer) { serializer.serializeValue(primaryKeyPropertyID); serializer.serializeUnorderedSet(fwdRelTableIDSet); @@ -21,8 +27,11 @@ std::unique_ptr NodeTableSchema::deserialize(Deserializer& dese deserializer.deserializeValue(primaryKeyPropertyID); deserializer.deserializeUnorderedSet(fwdRelTableIDSet); deserializer.deserializeUnorderedSet(bwdRelTableIDSet); - return std::make_unique( - primaryKeyPropertyID, fwdRelTableIDSet, bwdRelTableIDSet); + auto schema = std::make_unique(); + schema->primaryKeyPropertyID = primaryKeyPropertyID; + schema->fwdRelTableIDSet = std::move(fwdRelTableIDSet); + schema->bwdRelTableIDSet = std::move(bwdRelTableIDSet); + return schema; } } // namespace catalog diff --git a/src/catalog/rdf_graph_schema.cpp b/src/catalog/rdf_graph_schema.cpp index 7836123848..7e866db602 100644 --- a/src/catalog/rdf_graph_schema.cpp +++ b/src/catalog/rdf_graph_schema.cpp @@ -8,6 +8,13 @@ using namespace kuzu::common; namespace kuzu { namespace catalog { +RdfGraphSchema::RdfGraphSchema(const RdfGraphSchema& other) : TableSchema{other} { + resourceTableID = other.resourceTableID; + literalTableID = other.literalTableID; + resourceTripleTableID = other.resourceTripleTableID; + literalTripleTableID = other.literalTripleTableID; +} + void RdfGraphSchema::serializeInternal(Serializer& serializer) { serializer.serializeValue(resourceTableID); serializer.serializeValue(literalTableID); diff --git a/src/catalog/rel_table_group_schema.cpp b/src/catalog/rel_table_group_schema.cpp index 1f7a319f96..3a89ab2826 100644 --- a/src/catalog/rel_table_group_schema.cpp +++ b/src/catalog/rel_table_group_schema.cpp @@ -8,6 +8,10 @@ using namespace kuzu::common; namespace kuzu { namespace catalog { +RelTableGroupSchema::RelTableGroupSchema(const RelTableGroupSchema& other) : TableSchema{other} { + relTableIDs = other.relTableIDs; +} + void RelTableGroupSchema::serializeInternal(Serializer& serializer) { serializer.serializeVector(relTableIDs); } @@ -15,7 +19,9 @@ void RelTableGroupSchema::serializeInternal(Serializer& serializer) { std::unique_ptr RelTableGroupSchema::deserialize(Deserializer& deserializer) { std::vector relTableIDs; deserializer.deserializeVector(relTableIDs); - return std::make_unique(std::move(relTableIDs)); + auto schema = std::make_unique(); + schema->relTableIDs = std::move(relTableIDs); + return schema; } } // namespace catalog diff --git a/src/catalog/rel_table_schema.cpp b/src/catalog/rel_table_schema.cpp index 3a876ada6a..aa314a1840 100644 --- a/src/catalog/rel_table_schema.cpp +++ b/src/catalog/rel_table_schema.cpp @@ -1,6 +1,6 @@ #include "catalog/rel_table_schema.h" -#include "common/exception/catalog.h" +#include "common/exception/binder.h" #include "common/serializer/deserializer.h" #include "common/serializer/serializer.h" @@ -9,52 +9,55 @@ using namespace kuzu::common; namespace kuzu { namespace catalog { -RelMultiplicity getRelMultiplicityFromString(const std::string& relMultiplicityString) { - if ("ONE_ONE" == relMultiplicityString) { - return RelMultiplicity::ONE_ONE; - } else if ("MANY_ONE" == relMultiplicityString) { - return RelMultiplicity::MANY_ONE; - } else if ("ONE_MANY" == relMultiplicityString) { - return RelMultiplicity::ONE_MANY; - } else if ("MANY_MANY" == relMultiplicityString) { - return RelMultiplicity::MANY_MANY; +RelMultiplicity RelMultiplicityUtils::getFwd(const std::string& multiplicityStr) { + if ("ONE_ONE" == multiplicityStr || "ONE_MANY" == multiplicityStr) { + return RelMultiplicity::ONE; + } else if ("MANY_ONE" == multiplicityStr || "MANY_MANY" == multiplicityStr) { + return RelMultiplicity::MANY; } - throw CatalogException("Invalid relMultiplicity string '" + relMultiplicityString + "'."); + throw BinderException( + stringFormat("Cannot bind {} as relationship multiplicity.", multiplicityStr)); } -std::string getRelMultiplicityAsString(RelMultiplicity relMultiplicity) { - switch (relMultiplicity) { - case RelMultiplicity::MANY_MANY: { - return "MANY_MANY"; - } - case RelMultiplicity::MANY_ONE: { - return "MANY_ONE"; - } - case RelMultiplicity::ONE_ONE: { - return "ONE_ONE"; - } - case RelMultiplicity::ONE_MANY: { - return "ONE_MANY"; - } - default: - throw CatalogException("Cannot convert rel multiplicity to std::string."); +RelMultiplicity RelMultiplicityUtils::getBwd(const std::string& multiplicityStr) { + if ("ONE_ONE" == multiplicityStr || "MANY_ONE" == multiplicityStr) { + return RelMultiplicity::ONE; + } else if ("ONE_MANY" == multiplicityStr || "MANY_MANY" == multiplicityStr) { + return RelMultiplicity::MANY; } + throw BinderException( + stringFormat("Cannot bind {} as relationship multiplicity.", multiplicityStr)); +} + +RelTableSchema::RelTableSchema(const RelTableSchema& other) : TableSchema{other} { + srcMultiplicity = other.srcMultiplicity; + dstMultiplicity = other.dstMultiplicity; + srcTableID = other.srcTableID; + dstTableID = other.dstTableID; } void RelTableSchema::serializeInternal(Serializer& serializer) { - serializer.serializeValue(relMultiplicity); + serializer.serializeValue(srcMultiplicity); + serializer.serializeValue(dstMultiplicity); serializer.serializeValue(srcTableID); serializer.serializeValue(dstTableID); } std::unique_ptr RelTableSchema::deserialize(Deserializer& deserializer) { - RelMultiplicity relMultiplicity; + RelMultiplicity srcMultiplicity; + RelMultiplicity dstMultiplicity; table_id_t srcTableID; table_id_t dstTableID; - deserializer.deserializeValue(relMultiplicity); + deserializer.deserializeValue(srcMultiplicity); + deserializer.deserializeValue(dstMultiplicity); deserializer.deserializeValue(srcTableID); deserializer.deserializeValue(dstTableID); - return std::make_unique(relMultiplicity, srcTableID, dstTableID); + auto schema = std::make_unique(); + schema->srcMultiplicity = srcMultiplicity; + schema->dstMultiplicity = dstMultiplicity; + schema->srcTableID = srcTableID; + schema->dstTableID = dstTableID; + return schema; } } // namespace catalog diff --git a/src/catalog/table_schema.cpp b/src/catalog/table_schema.cpp index 47d1e81c75..17e05f7f7f 100644 --- a/src/catalog/table_schema.cpp +++ b/src/catalog/table_schema.cpp @@ -17,6 +17,15 @@ using namespace kuzu::common; namespace kuzu { namespace catalog { +TableSchema::TableSchema(const TableSchema& other) { + tableType = other.tableType; + tableName = other.tableName; + tableID = other.tableID; + properties = Property::copy(other.properties); + comment = other.comment; + nextPropertyID = other.nextPropertyID; +} + bool TableSchema::isReservedPropertyName(const std::string& propertyName) { return StringUtils::getUpper(propertyName) == InternalKeyword::ID; } @@ -37,13 +46,19 @@ bool TableSchema::containProperty(const std::string& propertyName) const { }); } -bool TableSchema::containsColumnType(const common::LogicalType& logicalType) const { +bool TableSchema::containPropertyType(const common::LogicalType& logicalType) const { return std::any_of(properties.begin(), properties.end(), [&logicalType](const std::unique_ptr& property) { return *property->getDataType() == logicalType; }); } +void TableSchema::addProperty( + std::string propertyName, std::unique_ptr dataType) { + properties.push_back(std::make_unique( + std::move(propertyName), std::move(dataType), nextPropertyID++, tableID)); +} + void TableSchema::dropProperty(common::property_id_t propertyID) { properties.erase(std::remove_if(properties.begin(), properties.end(), [propertyID](const std::unique_ptr& property) { diff --git a/src/include/binder/ddl/bound_create_table_info.h b/src/include/binder/ddl/bound_create_table_info.h index a660f64fd2..cf9b3b53ac 100644 --- a/src/include/binder/ddl/bound_create_table_info.h +++ b/src/include/binder/ddl/bound_create_table_info.h @@ -50,19 +50,22 @@ struct BoundExtraCreateNodeTableInfo : public BoundExtraCreateTableInfo { }; struct BoundExtraCreateRelTableInfo : public BoundExtraCreateTableInfo { - catalog::RelMultiplicity relMultiplicity; + catalog::RelMultiplicity srcMultiplicity; + catalog::RelMultiplicity dstMultiplicity; common::table_id_t srcTableID; common::table_id_t dstTableID; - std::vector> properties; + catalog::property_vector_t properties; - BoundExtraCreateRelTableInfo(catalog::RelMultiplicity relMultiplicity, - common::table_id_t srcTableID, common::table_id_t dstTableID, - std::vector> properties) - : relMultiplicity{relMultiplicity}, srcTableID{srcTableID}, dstTableID{dstTableID}, - properties{std::move(properties)} {} + BoundExtraCreateRelTableInfo(catalog::RelMultiplicity srcMultiplicity, + catalog::RelMultiplicity dstMultiplicity, common::table_id_t srcTableID, + common::table_id_t dstTableID, catalog::property_vector_t properties) + : srcMultiplicity{srcMultiplicity}, dstMultiplicity{dstMultiplicity}, + srcTableID{srcTableID}, dstTableID{dstTableID}, properties{std::move(properties)} {} BoundExtraCreateRelTableInfo(const BoundExtraCreateRelTableInfo& other) - : relMultiplicity{other.relMultiplicity}, srcTableID{other.srcTableID}, - dstTableID{other.dstTableID}, properties{catalog::Property::copy(other.properties)} {} + : srcMultiplicity{other.srcMultiplicity}, dstMultiplicity{other.dstMultiplicity}, + srcTableID{other.srcTableID}, dstTableID{other.dstTableID}, properties{ + catalog::Property::copy( + other.properties)} {} inline std::unique_ptr copy() const final { return std::make_unique(*this); diff --git a/src/include/catalog/node_table_schema.h b/src/include/catalog/node_table_schema.h index 655844a403..19fb50c860 100644 --- a/src/include/catalog/node_table_schema.h +++ b/src/include/catalog/node_table_schema.h @@ -1,6 +1,5 @@ #pragma once -#include "common/constants.h" #include "table_schema.h" namespace kuzu { @@ -8,48 +7,28 @@ namespace catalog { class NodeTableSchema : public TableSchema { public: - NodeTableSchema(common::property_id_t primaryPropertyId, - std::unordered_set fwdRelTableIDSet, - std::unordered_set bwdRelTableIDSet) - : TableSchema{common::InternalKeyword::ANONYMOUS, common::INVALID_TABLE_ID, - common::TableType::NODE, std::vector>{}}, - primaryKeyPropertyID{primaryPropertyId}, fwdRelTableIDSet{std::move(fwdRelTableIDSet)}, - bwdRelTableIDSet{std::move(bwdRelTableIDSet)} {} + NodeTableSchema() : TableSchema{common::TableType::NODE} {} NodeTableSchema(std::string tableName, common::table_id_t tableID, - common::property_id_t primaryPropertyId, std::vector> properties) + common::property_id_t primaryPropertyId, property_vector_t properties) : TableSchema{std::move(tableName), tableID, common::TableType::NODE, std::move(properties)}, primaryKeyPropertyID{primaryPropertyId} {} - NodeTableSchema(std::string tableName, common::table_id_t tableID, - std::vector> properties, std::string comment, - common::property_id_t nextPropertyID, common::property_id_t primaryKeyPropertyID, - std::unordered_set fwdRelTableIDSet, - std::unordered_set bwdRelTableIDSet) - : TableSchema{common::TableType::NODE, std::move(tableName), tableID, std::move(properties), - std::move(comment), nextPropertyID}, - primaryKeyPropertyID{primaryKeyPropertyID}, fwdRelTableIDSet{std::move(fwdRelTableIDSet)}, - bwdRelTableIDSet{std::move(bwdRelTableIDSet)} {} - - inline void addFwdRelTableID(common::table_id_t tableID) { fwdRelTableIDSet.insert(tableID); } - inline void addBwdRelTableID(common::table_id_t tableID) { bwdRelTableIDSet.insert(tableID); } + NodeTableSchema(const NodeTableSchema& other); + // TODO(Xiyang): this seems in correct; inline Property* getPrimaryKey() const { return properties[primaryKeyPropertyID].get(); } static std::unique_ptr deserialize(common::Deserializer& deserializer); inline common::property_id_t getPrimaryKeyPropertyID() const { return primaryKeyPropertyID; } - inline const std::unordered_set& getFwdRelTableIDSet() const { - return fwdRelTableIDSet; - } - - inline const std::unordered_set& getBwdRelTableIDSet() const { - return bwdRelTableIDSet; - } + inline void addFwdRelTableID(common::table_id_t tableID) { fwdRelTableIDSet.insert(tableID); } + inline void addBwdRelTableID(common::table_id_t tableID) { bwdRelTableIDSet.insert(tableID); } + inline const common::table_id_set_t& getFwdRelTableIDSet() const { return fwdRelTableIDSet; } + inline const common::table_id_set_t& getBwdRelTableIDSet() const { return bwdRelTableIDSet; } inline std::unique_ptr copy() const override { - return std::make_unique(tableName, tableID, Property::copy(properties), - comment, nextPropertyID, primaryKeyPropertyID, fwdRelTableIDSet, bwdRelTableIDSet); + return std::make_unique(*this); } private: @@ -61,8 +40,8 @@ class NodeTableSchema : public TableSchema { // information with the property). This is an idx, not an ID, so as the columns/properties of // the table change, the idx can change. common::property_id_t primaryKeyPropertyID; - std::unordered_set fwdRelTableIDSet; // srcNode->rel - std::unordered_set bwdRelTableIDSet; // dstNode->rel + common::table_id_set_t fwdRelTableIDSet; // srcNode->rel + common::table_id_set_t bwdRelTableIDSet; // dstNode->rel }; } // namespace catalog diff --git a/src/include/catalog/property.h b/src/include/catalog/property.h index 3574453d71..9a590a6580 100644 --- a/src/include/catalog/property.h +++ b/src/include/catalog/property.h @@ -9,12 +9,14 @@ class Deserializer; } // namespace common namespace catalog { +class Property; +using property_vector_t = std::vector>; + class Property { public: Property(std::string name, std::unique_ptr dataType) : Property{std::move(name), std::move(dataType), common::INVALID_PROPERTY_ID, common::INVALID_TABLE_ID} {} - Property(std::string name, std::unique_ptr dataType, common::property_id_t propertyID, common::table_id_t tableID) : name{std::move(name)}, dataType{std::move(dataType)}, @@ -32,7 +34,7 @@ class Property { inline void setTableID(common::table_id_t tableID_) { this->tableID = tableID_; } - inline void rename(std::string newName) { this->name = std::move(newName); } + inline void rename(std::string newName) { name = std::move(newName); } void serialize(common::Serializer& serializer) const; static std::unique_ptr deserialize(common::Deserializer& deserializer); diff --git a/src/include/catalog/rdf_graph_schema.h b/src/include/catalog/rdf_graph_schema.h index 1819287e8a..1a41a6f09f 100644 --- a/src/include/catalog/rdf_graph_schema.h +++ b/src/include/catalog/rdf_graph_schema.h @@ -1,6 +1,5 @@ #pragma once -#include "common/constants.h" #include "table_schema.h" namespace kuzu { @@ -8,17 +7,15 @@ namespace catalog { class RdfGraphSchema : public TableSchema { public: - RdfGraphSchema() - : TableSchema{common::InternalKeyword::ANONYMOUS, common::INVALID_TABLE_ID, - common::TableType::RDF, std::vector>{}} {} + RdfGraphSchema() : TableSchema{common::TableType::RDF} {} RdfGraphSchema(std::string tableName, common::table_id_t rdfID, common::table_id_t resourceTableID, common::table_id_t literalTabelID, common::table_id_t resourceTripleTableID, common::table_id_t literalTripleTableID) - : TableSchema{std::move(tableName), rdfID, common::TableType::RDF, - std::vector>{}}, + : TableSchema{std::move(tableName), rdfID, common::TableType::RDF, property_vector_t{}}, resourceTableID{resourceTableID}, literalTableID{literalTabelID}, resourceTripleTableID{resourceTripleTableID}, literalTripleTableID{literalTripleTableID} { } + RdfGraphSchema(const RdfGraphSchema& other); inline common::table_id_t getResourceTableID() const { return resourceTableID; } inline common::table_id_t getLiteralTableID() const { return literalTableID; } @@ -28,8 +25,7 @@ class RdfGraphSchema : public TableSchema { static std::unique_ptr deserialize(common::Deserializer& deserializer); inline std::unique_ptr copy() const final { - return std::make_unique(tableName, tableID, resourceTableID, literalTableID, - resourceTripleTableID, literalTripleTableID); + return std::make_unique(*this); } private: diff --git a/src/include/catalog/rel_table_group_schema.h b/src/include/catalog/rel_table_group_schema.h index c209a11dae..aea848f7b8 100644 --- a/src/include/catalog/rel_table_group_schema.h +++ b/src/include/catalog/rel_table_group_schema.h @@ -1,6 +1,5 @@ #pragma once -#include "common/constants.h" #include "table_schema.h" namespace kuzu { @@ -8,15 +7,13 @@ namespace catalog { class RelTableGroupSchema : public TableSchema { public: + RelTableGroupSchema() : TableSchema{common::TableType::REL_GROUP} {} RelTableGroupSchema(std::string tableName, common::table_id_t tableID, std::vector relTableIDs) : TableSchema{std::move(tableName), tableID, common::TableType::REL_GROUP, - std::vector>{}}, - relTableIDs{std::move(relTableIDs)} {} - explicit RelTableGroupSchema(std::vector relTableIDs) - : TableSchema{common::InternalKeyword::ANONYMOUS, common::INVALID_TABLE_ID, - common::TableType::REL_GROUP, std::vector>{}}, + property_vector_t{}}, relTableIDs{std::move(relTableIDs)} {} + RelTableGroupSchema(const RelTableGroupSchema& other); inline std::vector getRelTableIDs() const { return relTableIDs; } diff --git a/src/include/catalog/rel_table_schema.h b/src/include/catalog/rel_table_schema.h index 48cd981b4c..a07c5caa89 100644 --- a/src/include/catalog/rel_table_schema.h +++ b/src/include/catalog/rel_table_schema.h @@ -1,49 +1,38 @@ #pragma once -#include "common/constants.h" #include "common/enums/rel_direction.h" #include "table_schema.h" namespace kuzu { namespace catalog { -enum class RelMultiplicity : uint8_t { MANY_MANY, MANY_ONE, ONE_MANY, ONE_ONE }; -RelMultiplicity getRelMultiplicityFromString(const std::string& relMultiplicityString); -std::string getRelMultiplicityAsString(RelMultiplicity relMultiplicity); +enum class RelMultiplicity : uint8_t { MANY, ONE }; +struct RelMultiplicityUtils { + static RelMultiplicity getFwd(const std::string& multiplicityStr); + static RelMultiplicity getBwd(const std::string& multiplicityStr); +}; class RelTableSchema : public TableSchema { public: - static constexpr uint64_t INTERNAL_REL_ID_PROPERTY_ID = 0; - - RelTableSchema(RelMultiplicity relMultiplicity, common::table_id_t srcTableID, - common::table_id_t dstTableID) - : TableSchema{common::InternalKeyword::ANONYMOUS, common::INVALID_TABLE_ID, - common::TableType::REL, {} /* properties */}, - relMultiplicity{relMultiplicity}, srcTableID{srcTableID}, dstTableID{dstTableID} {} - RelTableSchema(std::string tableName, common::table_id_t tableID, - RelMultiplicity relMultiplicity, std::vector> properties, + RelTableSchema() : TableSchema(common::TableType::REL) {} + RelTableSchema(std::string tableName, common::table_id_t tableID, property_vector_t properties, + RelMultiplicity srcMultiplicity, RelMultiplicity dstMultiplicity, common::table_id_t srcTableID, common::table_id_t dstTableID) : TableSchema{std::move(tableName), tableID, common::TableType::REL, std::move(properties)}, - relMultiplicity{relMultiplicity}, srcTableID{srcTableID}, dstTableID{dstTableID} {} - RelTableSchema(std::string tableName, common::table_id_t tableID, - std::vector> properties, std::string comment, - common::property_id_t nextPropertyID, RelMultiplicity relMultiplicity, - common::table_id_t srcTableID, common::table_id_t dstTableID) - : TableSchema{common::TableType::REL, std::move(tableName), tableID, std::move(properties), - std::move(comment), nextPropertyID}, - relMultiplicity{relMultiplicity}, srcTableID{srcTableID}, dstTableID{dstTableID} {} + srcMultiplicity{srcMultiplicity}, dstMultiplicity{dstMultiplicity}, + srcTableID{srcTableID}, dstTableID{dstTableID} {} + RelTableSchema(const RelTableSchema& other); - inline bool isSingleMultiplicityInDirection(common::RelDataDirection direction) const { - return relMultiplicity == RelMultiplicity::ONE_ONE || - relMultiplicity == (direction == common::RelDataDirection::FWD ? - RelMultiplicity::MANY_ONE : - RelMultiplicity::ONE_MANY); + inline bool isSingleMultiplicity(common::RelDataDirection direction) const { + return getMultiplicity(direction) == RelMultiplicity::ONE; + } + inline RelMultiplicity getMultiplicity(common::RelDataDirection direction) const { + return direction == common::RelDataDirection::FWD ? dstMultiplicity : srcMultiplicity; } inline bool isSrcOrDstTable(common::table_id_t tableID) const { return srcTableID == tableID || dstTableID == tableID; } - inline common::table_id_t getBoundTableID(common::RelDataDirection relDirection) const { return relDirection == common::RelDataDirection::FWD ? srcTableID : dstTableID; } @@ -56,15 +45,15 @@ class RelTableSchema : public TableSchema { static std::unique_ptr deserialize(common::Deserializer& deserializer); inline std::unique_ptr copy() const override { - return std::make_unique(tableName, tableID, Property::copy(properties), - comment, nextPropertyID, relMultiplicity, srcTableID, dstTableID); + return std::make_unique(*this); } private: void serializeInternal(common::Serializer& serializer) final; private: - RelMultiplicity relMultiplicity; + RelMultiplicity srcMultiplicity; + RelMultiplicity dstMultiplicity; common::table_id_t srcTableID; common::table_id_t dstTableID; }; diff --git a/src/include/catalog/table_schema.h b/src/include/catalog/table_schema.h index 07b1ddbb80..f6513eb237 100644 --- a/src/include/catalog/table_schema.h +++ b/src/include/catalog/table_schema.h @@ -12,69 +12,54 @@ namespace catalog { class TableSchema { public: + explicit TableSchema(common::TableType tableType) + : tableType{tableType}, tableName{}, tableID{common::INVALID_TABLE_ID}, comment{}, + nextPropertyID{0} {} TableSchema(std::string tableName, common::table_id_t tableID, common::TableType tableType, std::vector> properties) : tableType{tableType}, tableName{std::move(tableName)}, tableID{tableID}, - properties{std::move(properties)}, comment{"" /* empty comment */}, + properties{std::move(properties)}, comment{}, nextPropertyID{(common::property_id_t)this->properties.size()} {} - TableSchema(common::TableType tableType, std::string tableName, common::table_id_t tableID, - std::vector> properties, std::string comment, - common::property_id_t nextPropertyID) - : tableType{tableType}, tableName{std::move(tableName)}, tableID{tableID}, - properties{std::move(properties)}, comment{std::move(comment)}, nextPropertyID{ - nextPropertyID} {} + TableSchema(const TableSchema& other); virtual ~TableSchema() = default; - static bool isReservedPropertyName(const std::string& propertyName); - + /* Table functions */ inline common::table_id_t getTableID() const { return tableID; } + inline common::TableType getTableType() const { return tableType; } + inline void renameTable(std::string newName) { tableName = std::move(newName); } + + inline void setComment(std::string newComment) { comment = std::move(newComment); } + + /* Property functions */ + static bool isReservedPropertyName(const std::string& propertyName); inline uint32_t getNumProperties() const { return properties.size(); } std::vector getProperties() const; bool containProperty(const std::string& propertyName) const; - bool containsColumnType(const common::LogicalType& logicalType) const; - void dropProperty(common::property_id_t propertyID); - - inline void addNodeProperty( - std::string propertyName, std::unique_ptr dataType) { - properties.push_back(std::make_unique( - std::move(propertyName), std::move(dataType), increaseNextPropertyID(), tableID)); - } - inline void addRelProperty( - std::string propertyName, std::unique_ptr dataType) { - properties.push_back(std::make_unique( - std::move(propertyName), std::move(dataType), increaseNextPropertyID(), tableID)); - } - + bool containPropertyType(const common::LogicalType& logicalType) const; common::property_id_t getPropertyID(const std::string& propertyName) const; - common::column_id_t getColumnID(common::property_id_t propertyID) const; - Property* getProperty(common::property_id_t propertyID) const; + common::column_id_t getColumnID(common::property_id_t propertyID) const; + + void addProperty(std::string propertyName, std::unique_ptr dataType); + void dropProperty(common::property_id_t propertyID); void renameProperty(common::property_id_t propertyID, const std::string& newName); void serialize(common::Serializer& serializer); static std::unique_ptr deserialize(common::Deserializer& deserializer); - inline common::TableType getTableType() const { return tableType; } - - inline void updateTableName(std::string newTableName) { tableName = std::move(newTableName); } - - inline void setComment(std::string newComment) { comment = std::move(newComment); } - virtual std::unique_ptr copy() const = 0; private: - inline common::property_id_t increaseNextPropertyID() { return nextPropertyID++; } - virtual void serializeInternal(common::Serializer& serializer) = 0; public: common::TableType tableType; std::string tableName; common::table_id_t tableID; - std::vector> properties; + property_vector_t properties; std::string comment; common::property_id_t nextPropertyID; }; diff --git a/src/include/storage/store/rel_table_data.h b/src/include/storage/store/rel_table_data.h index 746d9857cd..b3592f003d 100644 --- a/src/include/storage/store/rel_table_data.h +++ b/src/include/storage/store/rel_table_data.h @@ -124,9 +124,8 @@ class RelTableData final : public TableData { static inline common::ColumnDataFormat getDataFormatFromSchema( catalog::RelTableSchema* tableSchema, common::RelDataDirection direction) { - return tableSchema->isSingleMultiplicityInDirection(direction) ? - common::ColumnDataFormat::REGULAR : - common::ColumnDataFormat::CSR; + return tableSchema->isSingleMultiplicity(direction) ? common::ColumnDataFormat::REGULAR : + common::ColumnDataFormat::CSR; } static inline common::vector_idx_t getDataIdxFromDirection(common::RelDataDirection direction) { return direction == common::RelDataDirection::FWD ? 0 : 1; diff --git a/src/planner/plan/append_extend.cpp b/src/planner/plan/append_extend.cpp index fe83f63341..92c7a270ac 100644 --- a/src/planner/plan/append_extend.cpp +++ b/src/planner/plan/append_extend.cpp @@ -33,7 +33,7 @@ static bool extendHasAtMostOneNbrGuarantee(RelExpression& rel, NodeExpression& b auto relDirection = ExtendDirectionUtils::getRelDataDirection(direction); auto relTableSchema = reinterpret_cast( catalog.getTableSchema(&DUMMY_READ_TRANSACTION, rel.getSingleTableID())); - return relTableSchema->isSingleMultiplicityInDirection(relDirection); + return relTableSchema->isSingleMultiplicity(relDirection); } static std::unordered_set getBoundNodeTableIDSet( diff --git a/src/planner/plan/plan_copy.cpp b/src/planner/plan/plan_copy.cpp index 8f6abd556b..3fa33f7d8b 100644 --- a/src/planner/plan/plan_copy.cpp +++ b/src/planner/plan/plan_copy.cpp @@ -53,15 +53,13 @@ static void appendPartitioner(BoundCopyFromInfo* copyFromInfo, LogicalPlan& plan payloads.push_back(extraInfo->dstOffset); // Partitioner for FWD direction rel data. infos.push_back(std::make_unique(extraInfo->srcOffset, payloads, - tableSchema->isSingleMultiplicityInDirection(RelDataDirection::FWD) ? - ColumnDataFormat::REGULAR : - ColumnDataFormat::CSR, + tableSchema->isSingleMultiplicity(RelDataDirection::FWD) ? ColumnDataFormat::REGULAR : + ColumnDataFormat::CSR, tableSchema)); // Partitioner for BWD direction rel data. infos.push_back(std::make_unique(extraInfo->dstOffset, payloads, - tableSchema->isSingleMultiplicityInDirection(RelDataDirection::BWD) ? - ColumnDataFormat::REGULAR : - ColumnDataFormat::CSR, + tableSchema->isSingleMultiplicity(RelDataDirection::BWD) ? ColumnDataFormat::REGULAR : + ColumnDataFormat::CSR, tableSchema)); } break; default: { diff --git a/src/processor/map/map_copy_from.cpp b/src/processor/map/map_copy_from.cpp index 5a088a93fa..2b31a2e601 100644 --- a/src/processor/map/map_copy_from.cpp +++ b/src/processor/map/map_copy_from.cpp @@ -177,9 +177,8 @@ std::unique_ptr PlanMapper::createCopyRel( auto outFSchema = copyFrom->getSchema(); auto tableSchema = dynamic_cast(copyFromInfo->tableSchema); auto partitioningIdx = direction == RelDataDirection::FWD ? 0 : 1; - auto dataFormat = tableSchema->isSingleMultiplicityInDirection(direction) ? - ColumnDataFormat::REGULAR : - ColumnDataFormat::CSR; + auto dataFormat = tableSchema->isSingleMultiplicity(direction) ? ColumnDataFormat::REGULAR : + ColumnDataFormat::CSR; auto copyRelInfo = std::make_unique(tableSchema, partitioningIdx, direction, dataFormat, storageManager.getWAL(), storageManager.compressionEnabled()); return std::make_unique(std::move(copyRelInfo), std::move(partitionerSharedState), diff --git a/src/processor/map/map_extend.cpp b/src/processor/map/map_extend.cpp index c348652c39..711aa49e3b 100644 --- a/src/processor/map/map_extend.cpp +++ b/src/processor/map/map_extend.cpp @@ -106,7 +106,7 @@ std::unique_ptr PlanMapper::mapExtend(LogicalOperator* logical auto relDataDirection = ExtendDirectionUtils::getRelDataDirection(extendDirection); auto scanInfo = getRelTableScanInfo( tableSchema, relDataDirection, storageManager, extend->getProperties()); - if (tableSchema->isSingleMultiplicityInDirection(relDataDirection)) { + if (tableSchema->isSingleMultiplicity(relDataDirection)) { return std::make_unique(std::move(scanInfo), inNodeVectorPos, outVectorsPos, std::move(prevOperator), getOperatorID(), extend->getExpressionsForPrinting()); diff --git a/src/storage/stats/rel_table_statistics.cpp b/src/storage/stats/rel_table_statistics.cpp index 71533dc34a..cb3a8f1c41 100644 --- a/src/storage/stats/rel_table_statistics.cpp +++ b/src/storage/stats/rel_table_statistics.cpp @@ -16,11 +16,11 @@ RelTableStats::RelTableStats( BMFileHandle* metadataFH, const TableSchema& schema, BufferManager* bufferManager, WAL* wal) : TableStatistics{schema}, nextRelOffset{0} { const auto& relTableSchema = static_cast(schema); - if (!relTableSchema.isSingleMultiplicityInDirection(RelDataDirection::FWD)) { + if (!relTableSchema.isSingleMultiplicity(RelDataDirection::FWD)) { fwdCSROffsetMetadataDAHInfo = TablesStatistics::createMetadataDAHInfo( LogicalType{LogicalTypeID::INT64}, *metadataFH, bufferManager, wal); } - if (!relTableSchema.isSingleMultiplicityInDirection(RelDataDirection::BWD)) { + if (!relTableSchema.isSingleMultiplicity(RelDataDirection::BWD)) { bwdCSROffsetMetadataDAHInfo = TablesStatistics::createMetadataDAHInfo( LogicalType{LogicalTypeID::INT64}, *metadataFH, bufferManager, wal); } diff --git a/test/test_files/exceptions/catalog/catalog.test b/test/test_files/exceptions/catalog/catalog.test index fd0c4dc406..af1454b417 100644 --- a/test/test_files/exceptions/catalog/catalog.test +++ b/test/test_files/exceptions/catalog/catalog.test @@ -3,7 +3,7 @@ -- --CASE BindNonExistingFunction +-CASE CatalogExeception -STATEMENT MATCH (a:person) RETURN dummy(n) ---- error Catalog exception: DUMMY function does not exist. @@ -12,8 +12,7 @@ Catalog exception: DUMMY function does not exist. ---- error Catalog exception: DUMMY function does not exist. --CASE CreateRelTableInvalidRelMultiplicity -STATEMENT CREATE REL TABLE knows_post ( FROM person TO person, MANY_LOT) ---- error -Catalog exception: Invalid relMultiplicity string 'MANY_LOT'. +Binder exception: Cannot bind MANY_LOT as relationship multiplicity.