Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rework table schema constructors #2562

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
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 @@
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 @@
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
andyfengHKU marked this conversation as resolved.
Show resolved Hide resolved

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
65 changes: 34 additions & 31 deletions src/catalog/rel_table_schema.cpp
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -9,52 +9,55 @@
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 29 in src/catalog/rel_table_schema.cpp

View check run for this annotation

Codecov / codecov/patch

src/catalog/rel_table_schema.cpp#L28-L29

Added lines #L28 - L29 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
Loading