Skip to content

Commit

Permalink
rework table schema constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
andyfengHKU committed Dec 10, 2023
1 parent 8b7cffe commit 6108487
Show file tree
Hide file tree
Showing 24 changed files with 176 additions and 184 deletions.
4 changes: 2 additions & 2 deletions src/binder/bind/bind_copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ std::unique_ptr<BoundStatement> Binder::bindCopyNodeFrom(const Statement& statem
auto& copyStatement = reinterpret_cast<const CopyFrom&>(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<std::string> expectedColumnNames;
std::vector<std::unique_ptr<common::LogicalType>> expectedColumnTypes;
bindExpectedNodeColumns(
Expand All @@ -146,7 +146,7 @@ std::unique_ptr<BoundStatement> Binder::bindCopyRelFrom(const parser::Statement&
auto& copyStatement = reinterpret_cast<const CopyFrom&>(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<std::string> expectedColumnNames;
std::vector<std::unique_ptr<common::LogicalType>> expectedColumnTypes;
Expand Down
5 changes: 3 additions & 2 deletions src/binder/bind/bind_ddl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,14 @@ std::unique_ptr<BoundCreateTableInfo> 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<BoundExtraCreateRelTableInfo>(
relMultiplicity, srcTableID, dstTableID, std::move(boundProperties));
srcMultiplicity, dstMultiplicity, srcTableID, dstTableID, std::move(boundProperties));
return std::make_unique<BoundCreateTableInfo>(
TableType::REL, info->tableName, std::move(boundExtraInfo));
}
Expand Down
8 changes: 4 additions & 4 deletions src/binder/bind/ddl/bind_create_rdf_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ std::unique_ptr<BoundCreateTableInfo> Binder::bindCreateRdfGraphInfo(const Creat
resourceTripleProperties.push_back(
std::make_unique<Property>(std::string(rdf::PID), LogicalType::INTERNAL_ID()));
auto boundResourceTripleExtraInfo =
std::make_unique<BoundExtraCreateRelTableInfo>(RelMultiplicity::MANY_MANY, INVALID_TABLE_ID,
INVALID_TABLE_ID, std::move(resourceTripleProperties));
std::make_unique<BoundExtraCreateRelTableInfo>(RelMultiplicity::MANY, RelMultiplicity::MANY,
INVALID_TABLE_ID, INVALID_TABLE_ID, std::move(resourceTripleProperties));
auto boundResourceTripleCreateInfo = std::make_unique<BoundCreateTableInfo>(
TableType::REL, resourceTripleTableName, std::move(boundResourceTripleExtraInfo));
// Literal triple table.
Expand All @@ -67,8 +67,8 @@ std::unique_ptr<BoundCreateTableInfo> Binder::bindCreateRdfGraphInfo(const Creat
literalTripleProperties.push_back(
std::make_unique<Property>(std::string(rdf::PID), LogicalType::INTERNAL_ID()));
auto boundLiteralTripleExtraInfo =
std::make_unique<BoundExtraCreateRelTableInfo>(RelMultiplicity::MANY_MANY, INVALID_TABLE_ID,
INVALID_TABLE_ID, std::move(literalTripleProperties));
std::make_unique<BoundExtraCreateRelTableInfo>(RelMultiplicity::MANY, RelMultiplicity::MANY,
INVALID_TABLE_ID, INVALID_TABLE_ID, std::move(literalTripleProperties));
auto boundLiteralTripleCreateInfo = std::make_unique<BoundCreateTableInfo>(
TableType::REL, literalTripleTableName, std::move(boundLiteralTripleExtraInfo));
// Rdf table.
Expand Down
4 changes: 2 additions & 2 deletions src/catalog/catalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<LogicalType> 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<LogicalType> 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) {
Expand Down
8 changes: 4 additions & 4 deletions src/catalog/catalog_content.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<RelTableSchema>(info.tableName, tableID, extraInfo->relMultiplicity,
std::move(properties), extraInfo->srcTableID, extraInfo->dstTableID);
auto relTableSchema = std::make_unique<RelTableSchema>(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;
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 11 additions & 2 deletions src/catalog/node_table_schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -21,8 +27,11 @@ std::unique_ptr<NodeTableSchema> NodeTableSchema::deserialize(Deserializer& dese
deserializer.deserializeValue(primaryKeyPropertyID);
deserializer.deserializeUnorderedSet(fwdRelTableIDSet);
deserializer.deserializeUnorderedSet(bwdRelTableIDSet);
return std::make_unique<NodeTableSchema>(
primaryKeyPropertyID, fwdRelTableIDSet, bwdRelTableIDSet);
auto schema = std::make_unique<NodeTableSchema>();
schema->primaryKeyPropertyID = primaryKeyPropertyID;
schema->fwdRelTableIDSet = std::move(fwdRelTableIDSet);
schema->bwdRelTableIDSet = std::move(bwdRelTableIDSet);
return schema;
}

} // namespace catalog
Expand Down
7 changes: 7 additions & 0 deletions src/catalog/rdf_graph_schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Check warning on line 16 in src/catalog/rdf_graph_schema.cpp

View check run for this annotation

Codecov / codecov/patch

src/catalog/rdf_graph_schema.cpp#L16

Added line #L16 was not covered by tests

void RdfGraphSchema::serializeInternal(Serializer& serializer) {
serializer.serializeValue(resourceTableID);
serializer.serializeValue(literalTableID);
Expand Down
8 changes: 7 additions & 1 deletion src/catalog/rel_table_group_schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@ using namespace kuzu::common;
namespace kuzu {
namespace catalog {

RelTableGroupSchema::RelTableGroupSchema(const RelTableGroupSchema& other) : TableSchema{other} {
relTableIDs = other.relTableIDs;
}

Check warning on line 13 in src/catalog/rel_table_group_schema.cpp

View check run for this annotation

Codecov / codecov/patch

src/catalog/rel_table_group_schema.cpp#L11-L13

Added lines #L11 - L13 were not covered by tests

void RelTableGroupSchema::serializeInternal(Serializer& serializer) {
serializer.serializeVector(relTableIDs);
}

std::unique_ptr<RelTableGroupSchema> RelTableGroupSchema::deserialize(Deserializer& deserializer) {
std::vector<table_id_t> relTableIDs;
deserializer.deserializeVector(relTableIDs);
return std::make_unique<RelTableGroupSchema>(std::move(relTableIDs));
auto schema = std::make_unique<RelTableGroupSchema>();
schema->relTableIDs = std::move(relTableIDs);
return schema;
}

} // namespace catalog
Expand Down
64 changes: 34 additions & 30 deletions src/catalog/rel_table_schema.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "catalog/rel_table_schema.h"

#include "common/exception/binder.h"
#include "common/exception/catalog.h"
#include "common/serializer/deserializer.h"
#include "common/serializer/serializer.h"
Expand All @@ -9,52 +10,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));

Check warning on line 30 in src/catalog/rel_table_schema.cpp

View check run for this annotation

Codecov / codecov/patch

src/catalog/rel_table_schema.cpp#L29-L30

Added lines #L29 - L30 were not covered by tests
}

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> 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<RelTableSchema>(relMultiplicity, srcTableID, dstTableID);
auto schema = std::make_unique<RelTableSchema>();
schema->srcMultiplicity = srcMultiplicity;
schema->dstMultiplicity = dstMultiplicity;
schema->srcTableID = srcTableID;
schema->dstTableID = dstTableID;
return schema;
}

} // namespace catalog
Expand Down
17 changes: 16 additions & 1 deletion src/catalog/table_schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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>& property) {
return *property->getDataType() == logicalType;
});
}

void TableSchema::addProperty(
std::string propertyName, std::unique_ptr<common::LogicalType> dataType) {
properties.push_back(std::make_unique<Property>(
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>& property) {
Expand Down
21 changes: 12 additions & 9 deletions src/include/binder/ddl/bound_create_table_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::unique_ptr<catalog::Property>> properties;
catalog::property_vector_t properties;

BoundExtraCreateRelTableInfo(catalog::RelMultiplicity relMultiplicity,
common::table_id_t srcTableID, common::table_id_t dstTableID,
std::vector<std::unique_ptr<catalog::Property>> 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<BoundExtraCreateTableInfo> copy() const final {
return std::make_unique<BoundExtraCreateRelTableInfo>(*this);
Expand Down
42 changes: 11 additions & 31 deletions src/include/catalog/node_table_schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,28 @@ namespace catalog {

class NodeTableSchema : public TableSchema {
public:
NodeTableSchema(common::property_id_t primaryPropertyId,
std::unordered_set<common::table_id_t> fwdRelTableIDSet,
std::unordered_set<common::table_id_t> bwdRelTableIDSet)
: TableSchema{common::InternalKeyword::ANONYMOUS, common::INVALID_TABLE_ID,
common::TableType::NODE, std::vector<std::unique_ptr<Property>>{}},
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<std::unique_ptr<Property>> 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<std::unique_ptr<Property>> properties, std::string comment,
common::property_id_t nextPropertyID, common::property_id_t primaryKeyPropertyID,
std::unordered_set<common::table_id_t> fwdRelTableIDSet,
std::unordered_set<common::table_id_t> 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<NodeTableSchema> deserialize(common::Deserializer& deserializer);

inline common::property_id_t getPrimaryKeyPropertyID() const { return primaryKeyPropertyID; }

inline const std::unordered_set<common::table_id_t>& getFwdRelTableIDSet() const {
return fwdRelTableIDSet;
}

inline const std::unordered_set<common::table_id_t>& 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<TableSchema> copy() const override {
return std::make_unique<NodeTableSchema>(tableName, tableID, Property::copy(properties),
comment, nextPropertyID, primaryKeyPropertyID, fwdRelTableIDSet, bwdRelTableIDSet);
return std::make_unique<NodeTableSchema>(*this);
}

private:
Expand All @@ -61,8 +41,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<common::table_id_t> fwdRelTableIDSet; // srcNode->rel
std::unordered_set<common::table_id_t> bwdRelTableIDSet; // dstNode->rel
common::table_id_set_t fwdRelTableIDSet; // srcNode->rel
common::table_id_set_t bwdRelTableIDSet; // dstNode->rel
};

} // namespace catalog
Expand Down
Loading

0 comments on commit 6108487

Please sign in to comment.