Skip to content

Commit

Permalink
Merge pull request #2020 from kuzudb/refactor-catalog
Browse files Browse the repository at this point in the history
Split catalog table schemas
  • Loading branch information
andyfengHKU committed Sep 11, 2023
2 parents e030f73 + 5f2712b commit 89c8147
Show file tree
Hide file tree
Showing 45 changed files with 569 additions and 477 deletions.
2 changes: 2 additions & 0 deletions src/binder/bind/bind_ddl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "binder/ddl/bound_drop_table.h"
#include "binder/ddl/bound_rename_property.h"
#include "binder/ddl/bound_rename_table.h"
#include "catalog/node_table_schema.h"
#include "catalog/rel_table_schema.h"
#include "common/string_utils.h"
#include "parser/ddl/add_property.h"
#include "parser/ddl/create_table.h"
Expand Down
14 changes: 10 additions & 4 deletions src/binder/bind/bind_graph_pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "binder/binder.h"
#include "binder/expression/path_expression.h"
#include "binder/expression/property_expression.h"
#include "catalog/node_table_schema.h"
#include "catalog/rel_table_schema.h"

using namespace kuzu::common;
using namespace kuzu::parser;
Expand Down Expand Up @@ -121,7 +123,8 @@ static std::vector<table_id_t> pruneRelTableIDs(const Catalog& catalog_,
auto dstNodeTableIDs = dstNode.getTableIDsSet();
std::vector<table_id_t> result;
for (auto& relTableID : relTableIDs) {
auto relTableSchema = catalog_.getReadOnlyVersion()->getRelTableSchema(relTableID);
auto relTableSchema = reinterpret_cast<RelTableSchema*>(
catalog_.getReadOnlyVersion()->getTableSchema(relTableID));
if (!srcNodeTableIDs.contains(relTableSchema->getSrcTableID()) ||
!dstNodeTableIDs.contains(relTableSchema->getDstTableID())) {
continue;
Expand Down Expand Up @@ -278,7 +281,8 @@ std::shared_ptr<RelExpression> Binder::createRecursiveQueryRel(const parser::Rel
std::shared_ptr<NodeExpression> dstNode, RelDirectionType directionType) {
std::unordered_set<table_id_t> recursiveNodeTableIDs;
for (auto relTableID : tableIDs) {
auto relTableSchema = catalog.getReadOnlyVersion()->getRelTableSchema(relTableID);
auto relTableSchema = reinterpret_cast<RelTableSchema*>(
catalog.getReadOnlyVersion()->getTableSchema(relTableID));
recursiveNodeTableIDs.insert(relTableSchema->getSrcTableID());
recursiveNodeTableIDs.insert(relTableSchema->getDstTableID());
}
Expand Down Expand Up @@ -344,7 +348,8 @@ std::pair<uint64_t, uint64_t> Binder::bindVariableLengthRelBound(
void Binder::bindQueryRelProperties(RelExpression& rel) {
std::vector<RelTableSchema*> tableSchemas;
for (auto tableID : rel.getTableIDs()) {
tableSchemas.push_back(catalog.getReadOnlyVersion()->getRelTableSchema(tableID));
tableSchemas.push_back(reinterpret_cast<RelTableSchema*>(
catalog.getReadOnlyVersion()->getTableSchema(tableID)));
}
for (auto& [propertyName, propertySchemas] :
getRelPropertyNameAndPropertiesPairs(tableSchemas)) {
Expand Down Expand Up @@ -416,7 +421,8 @@ std::shared_ptr<NodeExpression> Binder::createQueryNode(
void Binder::bindQueryNodeProperties(NodeExpression& node) {
std::vector<NodeTableSchema*> tableSchemas;
for (auto tableID : node.getTableIDs()) {
tableSchemas.push_back(catalog.getReadOnlyVersion()->getNodeTableSchema(tableID));
tableSchemas.push_back(reinterpret_cast<NodeTableSchema*>(
catalog.getReadOnlyVersion()->getTableSchema(tableID)));
}
for (auto& [propertyName, propertySchemas] :
getNodePropertyNameAndPropertiesPairs(tableSchemas)) {
Expand Down
5 changes: 4 additions & 1 deletion src/binder/bind/bind_updating_clause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
#include "binder/query/updating_clause/bound_delete_clause.h"
#include "binder/query/updating_clause/bound_merge_clause.h"
#include "binder/query/updating_clause/bound_set_clause.h"
#include "catalog/node_table_schema.h"
#include "parser/query/updating_clause/create_clause.h"
#include "parser/query/updating_clause/delete_clause.h"
#include "parser/query/updating_clause/merge_clause.h"
#include "parser/query/updating_clause/set_clause.h"

using namespace kuzu::common;
using namespace kuzu::parser;
using namespace kuzu::catalog;

namespace kuzu {
namespace binder {
Expand Down Expand Up @@ -124,7 +126,8 @@ std::unique_ptr<BoundCreateInfo> Binder::bindCreateNodeInfo(
"Create node " + node->toString() + " with multiple node labels is not supported.");
}
auto nodeTableID = node->getSingleTableID();
auto nodeTableSchema = catalog.getReadOnlyVersion()->getNodeTableSchema(nodeTableID);
auto nodeTableSchema = reinterpret_cast<NodeTableSchema*>(
catalog.getReadOnlyVersion()->getTableSchema(nodeTableID));
auto primaryKey = nodeTableSchema->getPrimaryKey();
std::shared_ptr<Expression> primaryKeyExpression;
std::vector<expression_pair> setItems;
Expand Down
4 changes: 3 additions & 1 deletion src/binder/binder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "binder/bound_statement_rewriter.h"
#include "binder/expression/variable_expression.h"
#include "catalog/rel_table_schema.h"
#include "common/string_utils.h"

using namespace kuzu::common;
Expand Down Expand Up @@ -180,7 +181,8 @@ bool Binder::validateStringParsingOptionName(std::string& parsingOptionName) {
}

void Binder::validateNodeTableHasNoEdge(const Catalog& _catalog, table_id_t tableID) {
for (auto& relTableSchema : _catalog.getReadOnlyVersion()->getRelTableSchemas()) {
for (auto& tableSchema : _catalog.getReadOnlyVersion()->getRelTableSchemas()) {
auto relTableSchema = reinterpret_cast<RelTableSchema*>(tableSchema);
if (relTableSchema->isSrcOrDstTable(tableID)) {
throw BinderException(StringUtils::string_format(
"Cannot delete a node table with edges. It is on the edges of rel: {}.",
Expand Down
7 changes: 5 additions & 2 deletions src/catalog/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ add_library(kuzu_catalog
OBJECT
catalog.cpp
catalog_content.cpp
table_schema.cpp
node_table_schema.cpp
property.cpp
rel_table_group_schema.cpp)
rdf_graph_schema.cpp
rel_table_group_schema.cpp
rel_table_schema.cpp
table_schema.cpp)

set(ALL_OBJECT_FILES
${ALL_OBJECT_FILES} $<TARGET_OBJECTS:kuzu_catalog>
Expand Down
18 changes: 13 additions & 5 deletions src/catalog/catalog.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#include "catalog/catalog.h"

#include "catalog/node_table_schema.h"
#include "catalog/rdf_graph_schema.h"
#include "catalog/rel_table_group_schema.h"
#include "catalog/rel_table_schema.h"
#include "common/ser_deser.h"
#include "common/string_utils.h"
#include "storage/wal/wal.h"
#include "transaction/transaction_action.h"

using namespace kuzu::common;
using namespace kuzu::storage;
Expand Down Expand Up @@ -115,15 +120,18 @@ void Catalog::renameProperty(
catalogContentForWriteTrx->getTableSchema(tableID)->renameProperty(propertyID, newName);
}

std::unordered_set<RelTableSchema*> Catalog::getAllRelTableSchemasContainBoundTable(
std::unordered_set<TableSchema*> Catalog::getAllRelTableSchemasContainBoundTable(
table_id_t boundTableID) const {
std::unordered_set<RelTableSchema*> relTableSchemas;
auto nodeTableSchema = getReadOnlyVersion()->getNodeTableSchema(boundTableID);
std::unordered_set<TableSchema*> relTableSchemas;
auto nodeTableSchema =
reinterpret_cast<NodeTableSchema*>(getReadOnlyVersion()->getTableSchema(boundTableID));
for (auto& fwdRelTableID : nodeTableSchema->getFwdRelTableIDSet()) {
relTableSchemas.insert(getReadOnlyVersion()->getRelTableSchema(fwdRelTableID));
relTableSchemas.insert(
reinterpret_cast<RelTableSchema*>(getReadOnlyVersion()->getTableSchema(fwdRelTableID)));
}
for (auto& bwdRelTableID : nodeTableSchema->getBwdRelTableIDSet()) {
relTableSchemas.insert(getReadOnlyVersion()->getRelTableSchema(bwdRelTableID));
relTableSchemas.insert(
reinterpret_cast<RelTableSchema*>(getReadOnlyVersion()->getTableSchema(bwdRelTableID)));
}
return relTableSchemas;
}
Expand Down
50 changes: 23 additions & 27 deletions src/catalog/catalog_content.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "catalog/catalog_content.h"

#include "catalog/node_table_schema.h"
#include "catalog/rdf_graph_schema.h"
#include "catalog/rel_table_group_schema.h"
#include "catalog/rel_table_schema.h"
#include "common/ser_deser.h"
#include "common/string_utils.h"
#include "storage/storage_utils.h"
Expand Down Expand Up @@ -55,8 +58,12 @@ table_id_t CatalogContent::addRelTableSchema(const BoundCreateTableInfo& info) {
auto properties = Property::copy(extraInfo->properties);
addRelInternalIDProperty(properties);
assignPropertyIDAndTableID(properties, tableID);
getNodeTableSchema(extraInfo->srcTableID)->addFwdRelTableID(tableID);
getNodeTableSchema(extraInfo->dstTableID)->addBwdRelTableID(tableID);
auto srcNodeTableSchema =
reinterpret_cast<NodeTableSchema*>(getTableSchema(extraInfo->srcTableID));
auto dstNodeTableSchema =
reinterpret_cast<NodeTableSchema*>(getTableSchema(extraInfo->dstTableID));
srcNodeTableSchema->addFwdRelTableID(tableID);
dstNodeTableSchema->addBwdRelTableID(tableID);
auto relTableSchema = std::make_unique<RelTableSchema>(info.tableName, tableID,
extraInfo->relMultiplicity, std::move(properties), extraInfo->srcTableID,
extraInfo->dstTableID, extraInfo->srcPkDataType->copy(), extraInfo->dstPkDataType->copy());
Expand Down Expand Up @@ -138,27 +145,6 @@ Property* CatalogContent::getRelProperty(
throw CatalogException("Cannot find rel property " + propertyName + ".");
}

std::vector<NodeTableSchema*> CatalogContent::getNodeTableSchemas() const {
std::vector<NodeTableSchema*> nodeTableSchemas;
for (auto& tableSchema : tableSchemas) {
if (tableSchema.second->getTableType() == TableType::NODE) {
nodeTableSchemas.push_back(
reinterpret_cast<NodeTableSchema*>(tableSchema.second.get()));
}
}
return nodeTableSchemas;
}

std::vector<RelTableSchema*> CatalogContent::getRelTableSchemas() const {
std::vector<RelTableSchema*> relTableSchemas;
for (auto& tableSchema : tableSchemas) {
if (tableSchema.second->getTableType() == TableType::REL) {
relTableSchemas.push_back(reinterpret_cast<RelTableSchema*>(tableSchema.second.get()));
}
}
return relTableSchemas;
}

void CatalogContent::dropTableSchema(table_id_t tableID) {
auto tableName = getTableName(tableID);
tableNameToIDMap.erase(tableName);
Expand Down Expand Up @@ -282,11 +268,21 @@ void CatalogContent::registerBuiltInFunctions() {
builtInTableFunctions = std::make_unique<function::BuiltInTableFunctions>();
}

std::vector<table_id_t> CatalogContent::getTableIDsByType(TableType tableType) const {
std::vector<TableSchema*> CatalogContent::getTableSchemas(common::TableType tableType) const {
std::vector<TableSchema*> result;
for (auto& [id, schema] : tableSchemas) {
if (schema->getTableType() == tableType) {
result.push_back(schema.get());
}
}
return result;
}

std::vector<table_id_t> CatalogContent::getTableIDs(TableType tableType) const {
std::vector<table_id_t> tableIDs;
for (auto& tableSchema : tableSchemas) {
if (tableSchema.second->getTableType() == tableType) {
tableIDs.push_back(tableSchema.first);
for (auto& [id, schema] : tableSchemas) {
if (schema->getTableType() == tableType) {
tableIDs.push_back(id);
}
}
return tableIDs;
Expand Down
29 changes: 29 additions & 0 deletions src/catalog/node_table_schema.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "catalog/node_table_schema.h"

#include "common/ser_deser.h"

using namespace kuzu::common;

namespace kuzu {
namespace catalog {

void NodeTableSchema::serializeInternal(FileInfo* fileInfo, uint64_t& offset) {
SerDeser::serializeValue(primaryKeyPropertyID, fileInfo, offset);
SerDeser::serializeUnorderedSet(fwdRelTableIDSet, fileInfo, offset);
SerDeser::serializeUnorderedSet(bwdRelTableIDSet, fileInfo, offset);
}

std::unique_ptr<NodeTableSchema> NodeTableSchema::deserialize(
FileInfo* fileInfo, uint64_t& offset) {
property_id_t primaryKeyPropertyID;
std::unordered_set<table_id_t> fwdRelTableIDSet;
std::unordered_set<table_id_t> bwdRelTableIDSet;
SerDeser::deserializeValue(primaryKeyPropertyID, fileInfo, offset);
SerDeser::deserializeUnorderedSet(fwdRelTableIDSet, fileInfo, offset);
SerDeser::deserializeUnorderedSet(bwdRelTableIDSet, fileInfo, offset);
return std::make_unique<NodeTableSchema>(
primaryKeyPropertyID, fwdRelTableIDSet, bwdRelTableIDSet);
}

} // namespace catalog
} // namespace kuzu
1 change: 1 addition & 0 deletions src/catalog/property.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ std::unique_ptr<Property> Property::deserialize(FileInfo* fileInfo, uint64_t& of
std::vector<std::unique_ptr<Property>> Property::copy(
const std::vector<std::unique_ptr<Property>>& properties) {
std::vector<std::unique_ptr<Property>> result;
result.reserve(properties.size());
for (auto& property : properties) {
result.push_back(property->copy());
}
Expand Down
24 changes: 24 additions & 0 deletions src/catalog/rdf_graph_schema.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "catalog/rdf_graph_schema.h"

#include "common/ser_deser.h"

using namespace kuzu::common;

namespace kuzu {
namespace catalog {

void RdfGraphSchema::serializeInternal(FileInfo* fileInfo, uint64_t& offset) {
SerDeser::serializeValue(nodeTableID, fileInfo, offset);
SerDeser::serializeValue(relTableID, fileInfo, offset);
}

std::unique_ptr<RdfGraphSchema> RdfGraphSchema::deserialize(FileInfo* fileInfo, uint64_t& offset) {
table_id_t nodeTableID;
table_id_t relTableID;
SerDeser::deserializeValue(nodeTableID, fileInfo, offset);
SerDeser::deserializeValue(relTableID, fileInfo, offset);
return std::make_unique<RdfGraphSchema>(nodeTableID, relTableID);
}

} // namespace catalog
} // namespace kuzu
65 changes: 65 additions & 0 deletions src/catalog/rel_table_schema.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "catalog/rel_table_schema.h"

#include "common/exception.h"
#include "common/ser_deser.h"

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;
}
throw CatalogException("Invalid relMultiplicity string '" + relMultiplicityString + "'.");
}

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.");
}
}

void RelTableSchema::serializeInternal(FileInfo* fileInfo, uint64_t& offset) {
SerDeser::serializeValue(relMultiplicity, fileInfo, offset);
SerDeser::serializeValue(srcTableID, fileInfo, offset);
SerDeser::serializeValue(dstTableID, fileInfo, offset);
srcPKDataType->serialize(fileInfo, offset);
dstPKDataType->serialize(fileInfo, offset);
}

std::unique_ptr<RelTableSchema> RelTableSchema::deserialize(FileInfo* fileInfo, uint64_t& offset) {
RelMultiplicity relMultiplicity;
table_id_t srcTableID;
table_id_t dstTableID;
SerDeser::deserializeValue(relMultiplicity, fileInfo, offset);
SerDeser::deserializeValue(srcTableID, fileInfo, offset);
SerDeser::deserializeValue(dstTableID, fileInfo, offset);
auto srcPKDataType = LogicalType::deserialize(fileInfo, offset);
auto dstPKDataType = LogicalType::deserialize(fileInfo, offset);
return std::make_unique<RelTableSchema>(relMultiplicity, srcTableID, dstTableID,
std::move(srcPKDataType), std::move(dstPKDataType));
}

} // namespace catalog
} // namespace kuzu
Loading

0 comments on commit 89c8147

Please sign in to comment.