Skip to content

Commit

Permalink
Refactor: unify many_one and many_many storage (#2912)
Browse files Browse the repository at this point in the history
remove many_one multiplicity in storage
  • Loading branch information
ray6080 authored Mar 3, 2024
1 parent df5ec1c commit f9e9e29
Show file tree
Hide file tree
Showing 67 changed files with 1,407 additions and 1,955 deletions.
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.3.1 LANGUAGES CXX C)
project(Kuzu VERSION 0.3.1.1 LANGUAGES CXX C)

find_package(Threads REQUIRED)

Expand Down
13 changes: 6 additions & 7 deletions src/binder/bind/ddl/bind_create_rdf_graph.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "binder/binder.h"
#include "catalog/catalog_entry/rdf_graph_catalog_entry.h"
#include "catalog/catalog_entry/rel_table_catalog_entry.h"
#include "common/keyword/rdf_keyword.h"
#include "parser/ddl/create_table_info.h"

Expand Down Expand Up @@ -36,19 +35,19 @@ BoundCreateTableInfo Binder::bindCreateRdfGraphInfo(const CreateTableInfo* info)
std::vector<PropertyInfo> resourceTripleProperties;
resourceTripleProperties.emplace_back(InternalKeyword::ID, *LogicalType::INTERNAL_ID());
resourceTripleProperties.emplace_back(std::string(rdf::PID), *LogicalType::INTERNAL_ID());
auto boundResourceTripleExtraInfo =
std::make_unique<BoundExtraCreateRelTableInfo>(RelMultiplicity::MANY, RelMultiplicity::MANY,
INVALID_TABLE_ID, INVALID_TABLE_ID, std::move(resourceTripleProperties));
auto boundResourceTripleExtraInfo = std::make_unique<BoundExtraCreateRelTableInfo>(
common::RelMultiplicity::MANY, common::RelMultiplicity::MANY, INVALID_TABLE_ID,
INVALID_TABLE_ID, std::move(resourceTripleProperties));
auto boundResourceTripleCreateInfo = BoundCreateTableInfo(
TableType::REL, resourceTripleTableName, std::move(boundResourceTripleExtraInfo));
// Literal triple table.
auto literalTripleTableName = RDFGraphCatalogEntry::getLiteralTripleTableName(rdfGraphName);
std::vector<PropertyInfo> literalTripleProperties;
literalTripleProperties.emplace_back(InternalKeyword::ID, *LogicalType::INTERNAL_ID());
literalTripleProperties.emplace_back(std::string(rdf::PID), *LogicalType::INTERNAL_ID());
auto boundLiteralTripleExtraInfo =
std::make_unique<BoundExtraCreateRelTableInfo>(RelMultiplicity::MANY, RelMultiplicity::MANY,
INVALID_TABLE_ID, INVALID_TABLE_ID, std::move(literalTripleProperties));
auto boundLiteralTripleExtraInfo = std::make_unique<BoundExtraCreateRelTableInfo>(
common::RelMultiplicity::MANY, common::RelMultiplicity::MANY, INVALID_TABLE_ID,
INVALID_TABLE_ID, std::move(literalTripleProperties));
auto boundLiteralTripleCreateInfo = BoundCreateTableInfo(
TableType::REL, literalTripleTableName, std::move(boundLiteralTripleExtraInfo));
// Rdf table.
Expand Down
40 changes: 9 additions & 31 deletions src/catalog/catalog_entry/rel_table_catalog_entry.cpp
Original file line number Diff line number Diff line change
@@ -1,36 +1,13 @@
#include "catalog/catalog_entry/rel_table_catalog_entry.h"

#include "catalog/catalog.h"
#include "common/exception/binder.h"

namespace kuzu {
namespace catalog {

using namespace kuzu::common;

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 BinderException(
stringFormat("Cannot bind {} as relationship multiplicity.", multiplicityStr));
}

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

RelTableCatalogEntry::RelTableCatalogEntry(std::string name, common::table_id_t tableID,
RelMultiplicity srcMultiplicity, RelMultiplicity dstMultiplicity, common::table_id_t srcTableID,
common::table_id_t dstTableID)
common::RelMultiplicity srcMultiplicity, common::RelMultiplicity dstMultiplicity,
common::table_id_t srcTableID, common::table_id_t dstTableID)
: TableCatalogEntry{CatalogEntryType::REL_TABLE_ENTRY, std::move(name), tableID},
srcMultiplicity{srcMultiplicity}, dstMultiplicity{dstMultiplicity}, srcTableID{srcTableID},
dstTableID{dstTableID} {}
Expand All @@ -48,9 +25,10 @@ bool RelTableCatalogEntry::isParent(common::table_id_t tableID) {
}

bool RelTableCatalogEntry::isSingleMultiplicity(common::RelDataDirection direction) const {
return getMultiplicity(direction) == RelMultiplicity::ONE;
return getMultiplicity(direction) == common::RelMultiplicity::ONE;
}
RelMultiplicity RelTableCatalogEntry::getMultiplicity(common::RelDataDirection direction) const {
common::RelMultiplicity RelTableCatalogEntry::getMultiplicity(
common::RelDataDirection direction) const {
return direction == common::RelDataDirection::FWD ? dstMultiplicity : srcMultiplicity;
}
common::table_id_t RelTableCatalogEntry::getBoundTableID(
Expand All @@ -72,8 +50,8 @@ void RelTableCatalogEntry::serialize(common::Serializer& serializer) const {

std::unique_ptr<RelTableCatalogEntry> RelTableCatalogEntry::deserialize(
common::Deserializer& deserializer) {
RelMultiplicity srcMultiplicity;
RelMultiplicity dstMultiplicity;
common::RelMultiplicity srcMultiplicity;
common::RelMultiplicity dstMultiplicity;
common::table_id_t srcTableID;
common::table_id_t dstTableID;
deserializer.deserializeValue(srcMultiplicity);
Expand All @@ -100,8 +78,8 @@ std::string RelTableCatalogEntry::toCypher(main::ClientContext* clientContext) c
ss << "CREATE REL TABLE " << getName() << "( FROM " << srcTableName << " TO " << dstTableName
<< ", ";
Property::toCypher(getPropertiesRef(), ss);
auto srcMultiStr = srcMultiplicity == RelMultiplicity::MANY ? "MANY" : "ONE";
auto dstMultiStr = dstMultiplicity == RelMultiplicity::MANY ? "MANY" : "ONE";
auto srcMultiStr = srcMultiplicity == common::RelMultiplicity::MANY ? "MANY" : "ONE";
auto dstMultiStr = dstMultiplicity == common::RelMultiplicity::MANY ? "MANY" : "ONE";
ss << srcMultiStr << "_" << dstMultiStr << ");";
return ss.str();
}
Expand Down
1 change: 1 addition & 0 deletions src/common/enums/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
add_library(kuzu_common_enums
OBJECT
rel_direction.cpp
rel_multiplicity.cpp
table_type.cpp)

set(ALL_OBJECT_FILES
Expand Down
30 changes: 30 additions & 0 deletions src/common/enums/rel_multiplicity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "common/enums/rel_multiplicity.h"

#include "common/exception/binder.h"
#include "common/string_format.h"

namespace kuzu {
namespace common {

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 BinderException(
stringFormat("Cannot bind {} as relationship multiplicity.", multiplicityStr));
}

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

} // namespace common
} // namespace kuzu
19 changes: 14 additions & 5 deletions src/common/exception/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,25 @@ std::string ExceptionMessage::overLargeStringPKValueException(uint64_t length) {
"string's length was {}.",
length);
}

std::string ExceptionMessage::overLargeStringValueException(uint64_t length) {
return stringFormat(
"The maximum length of strings is 262144 bytes. The input string's length was {}.", length);
}

std::string ExceptionMessage::violateUniquenessOfRelAdjColumn(const std::string& tableName,
const std::string& offset, const std::string& multiplicity, const std::string& direction) {
return stringFormat("RelTable {} is a {} table, but node(nodeOffset: {}) "
"has more than one neighbour in the {} direction.",
tableName, offset, multiplicity, direction);
std::string ExceptionMessage::violateDeleteNodeWithConnectedEdgesConstraint(
const std::string& tableName, const std::string& offset, const std::string& direction) {
return stringFormat(
"Node(nodeOffset: {}) has connected edges in table {} in the {} direction, "
"which cannot be deleted. Please delete the edges first or try DETACH DELETE.",
offset, tableName, direction);
}

std::string ExceptionMessage::violateRelMultiplicityConstraint(
const std::string& tableName, const std::string& offset, const std::string& direction) {
return stringFormat("Node(nodeOffset: {}) has more than one neighbour in table {} in the {} "
"direction, which violates the rel multiplicity constraint.",
offset, tableName, direction);
}

std::string ExceptionMessage::validateCopyNpyNotForRelTablesException(
Expand Down
12 changes: 4 additions & 8 deletions src/function/table/call/storage_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,10 @@ struct StorageInfoSharedState final : public CallFuncSharedState {
} break;
case TableType::REL: {
auto relTable = ku_dynamic_cast<Table*, RelTable*>(table);
if (relTable->getTableDataFormat(RelDataDirection::FWD) == ColumnDataFormat::CSR) {
columns.push_back(relTable->getCSROffsetColumn(RelDataDirection::FWD));
columns.push_back(relTable->getCSRLengthColumn(RelDataDirection::FWD));
}
if (relTable->getTableDataFormat(RelDataDirection::BWD) == ColumnDataFormat::CSR) {
columns.push_back(relTable->getCSROffsetColumn(RelDataDirection::BWD));
columns.push_back(relTable->getCSRLengthColumn(RelDataDirection::BWD));
}
columns.push_back(relTable->getCSROffsetColumn(RelDataDirection::FWD));
columns.push_back(relTable->getCSRLengthColumn(RelDataDirection::FWD));
columns.push_back(relTable->getCSROffsetColumn(RelDataDirection::BWD));
columns.push_back(relTable->getCSRLengthColumn(RelDataDirection::BWD));
columns.push_back(relTable->getAdjColumn(RelDataDirection::FWD));
columns.push_back(relTable->getAdjColumn(RelDataDirection::BWD));
for (auto columnID = 0u; columnID < relTable->getNumColumns(); columnID++) {
Expand Down
9 changes: 5 additions & 4 deletions src/include/binder/ddl/bound_create_table_info.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "common/copy_constructors.h"
#include "common/enums/rel_multiplicity.h"
#include "common/enums/table_type.h"
#include "common/types/types.h"

Expand Down Expand Up @@ -58,14 +59,14 @@ struct BoundExtraCreateNodeTableInfo : public BoundExtraCreateTableInfo {
};

struct BoundExtraCreateRelTableInfo : public BoundExtraCreateTableInfo {
catalog::RelMultiplicity srcMultiplicity;
catalog::RelMultiplicity dstMultiplicity;
common::RelMultiplicity srcMultiplicity;
common::RelMultiplicity dstMultiplicity;
common::table_id_t srcTableID;
common::table_id_t dstTableID;
std::vector<PropertyInfo> propertyInfos;

BoundExtraCreateRelTableInfo(catalog::RelMultiplicity srcMultiplicity,
catalog::RelMultiplicity dstMultiplicity, common::table_id_t srcTableID,
BoundExtraCreateRelTableInfo(common::RelMultiplicity srcMultiplicity,
common::RelMultiplicity dstMultiplicity, common::table_id_t srcTableID,
common::table_id_t dstTableID, std::vector<PropertyInfo> propertyInfos)
: srcMultiplicity{srcMultiplicity}, dstMultiplicity{dstMultiplicity},
srcTableID{srcTableID}, dstTableID{dstTableID}, propertyInfos{std::move(propertyInfos)} {}
Expand Down
15 changes: 5 additions & 10 deletions src/include/catalog/catalog_entry/rel_table_catalog_entry.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
#pragma once

#include "common/enums/rel_direction.h"
#include "common/enums/rel_multiplicity.h"
#include "table_catalog_entry.h"

namespace kuzu {
namespace catalog {

enum class RelMultiplicity : uint8_t { MANY, ONE };
struct RelMultiplicityUtils {
static RelMultiplicity getFwd(const std::string& multiplicityStr);
static RelMultiplicity getBwd(const std::string& multiplicityStr);
};

class RelTableCatalogEntry final : public TableCatalogEntry {
public:
//===--------------------------------------------------------------------===//
// constructors
//===--------------------------------------------------------------------===//
RelTableCatalogEntry() = default;
RelTableCatalogEntry(std::string name, common::table_id_t tableID,
RelMultiplicity srcMultiplicity, RelMultiplicity dstMultiplicity,
common::RelMultiplicity srcMultiplicity, common::RelMultiplicity dstMultiplicity,
common::table_id_t srcTableID, common::table_id_t dstTableID);
RelTableCatalogEntry(const RelTableCatalogEntry& other);

Expand All @@ -31,7 +26,7 @@ class RelTableCatalogEntry final : public TableCatalogEntry {
common::table_id_t getSrcTableID() const { return srcTableID; }
common::table_id_t getDstTableID() const { return dstTableID; }
bool isSingleMultiplicity(common::RelDataDirection direction) const;
RelMultiplicity getMultiplicity(common::RelDataDirection direction) const;
common::RelMultiplicity getMultiplicity(common::RelDataDirection direction) const;
common::table_id_t getBoundTableID(common::RelDataDirection relDirection) const;
common::table_id_t getNbrTableID(common::RelDataDirection relDirection) const;

Expand All @@ -44,8 +39,8 @@ class RelTableCatalogEntry final : public TableCatalogEntry {
std::string toCypher(main::ClientContext* clientContext) const override;

private:
RelMultiplicity srcMultiplicity;
RelMultiplicity dstMultiplicity;
common::RelMultiplicity srcMultiplicity;
common::RelMultiplicity dstMultiplicity;
common::table_id_t srcTableID;
common::table_id_t dstTableID;
};
Expand Down
16 changes: 16 additions & 0 deletions src/include/common/enums/rel_multiplicity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <cstdint>
#include <string>

namespace kuzu {
namespace common {

enum class RelMultiplicity : uint8_t { MANY, ONE };
struct RelMultiplicityUtils {
static RelMultiplicity getFwd(const std::string& multiplicityStr);
static RelMultiplicity getBwd(const std::string& multiplicityStr);
};

} // namespace common
} // namespace kuzu
6 changes: 4 additions & 2 deletions src/include/common/exception/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ struct ExceptionMessage {
}
static std::string overLargeStringPKValueException(uint64_t length);
static std::string overLargeStringValueException(uint64_t length);
static std::string violateUniquenessOfRelAdjColumn(const std::string& tableName,
const std::string& offset, const std::string& multiplicity, const std::string& direction);
static std::string violateDeleteNodeWithConnectedEdgesConstraint(
const std::string& tableName, const std::string& offset, const std::string& direction);
static std::string violateRelMultiplicityConstraint(
const std::string& tableName, const std::string& offset, const std::string& direction);

static inline std::string validateCopyNPYByColumnException() {
return "Please use COPY FROM BY COLUMN statement for copying npy files.";
Expand Down
25 changes: 14 additions & 11 deletions src/include/processor/operator/persistent/copy_rel.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,18 @@ struct CopyRelInfo {
catalog::RelTableCatalogEntry* relTableEntry;
common::vector_idx_t partitioningIdx;
common::RelDataDirection dataDirection;
common::ColumnDataFormat dataFormat;

storage::WAL* wal;
bool compressionEnabled;

CopyRelInfo(catalog::RelTableCatalogEntry* relTableEntry, common::vector_idx_t partitioningIdx,
common::RelDataDirection dataDirection, common::ColumnDataFormat dataFormat,
storage::WAL* wal, bool compressionEnabled)
common::RelDataDirection dataDirection, storage::WAL* wal, bool compressionEnabled)
: relTableEntry{relTableEntry}, partitioningIdx{partitioningIdx},
dataDirection{dataDirection}, dataFormat{dataFormat}, wal{wal}, compressionEnabled{
compressionEnabled} {}
dataDirection{dataDirection}, wal{wal}, compressionEnabled{compressionEnabled} {}
CopyRelInfo(const CopyRelInfo& other)
: relTableEntry{other.relTableEntry}, partitioningIdx{other.partitioningIdx},
dataDirection{other.dataDirection}, dataFormat{other.dataFormat}, wal{other.wal},
compressionEnabled{other.compressionEnabled} {}
dataDirection{other.dataDirection}, wal{other.wal}, compressionEnabled{
other.compressionEnabled} {}

inline std::unique_ptr<CopyRelInfo> copy() { return std::make_unique<CopyRelInfo>(*this); }
};
Expand Down Expand Up @@ -95,10 +92,12 @@ class CopyRel : public Sink {
}

void prepareCSRNodeGroup(common::DataChunkCollection* partition,
common::vector_idx_t offsetVectorIdx, common::offset_t numNodes);
common::offset_t startNodeOffset, common::vector_idx_t offsetVectorIdx,
common::offset_t numNodes);

static void populateStartCSROffsetsAndLengths(storage::CSRHeaderChunks& csrHeader,
std::vector<common::offset_t>& gaps, common::offset_t numNodes,
static common::length_t getGapSize(common::length_t length);
static std::vector<common::offset_t> populateStartCSROffsetsAndLengths(
storage::CSRHeaderChunks& csrHeader, common::offset_t numNodes,
common::DataChunkCollection* partition, common::vector_idx_t offsetVectorIdx);
static void populateEndCSROffsets(
storage::CSRHeaderChunks& csrHeader, std::vector<common::offset_t>& gaps);
Expand All @@ -107,7 +106,11 @@ class CopyRel : public Sink {
static void setOffsetFromCSROffsets(
common::ValueVector* offsetVector, storage::ColumnChunk* offsetChunk);

protected:
// We only check rel multiplcity constraint (MANY_ONE, ONE_ONE) for now.
std::optional<common::offset_t> checkRelMultiplicityConstraint(
const storage::CSRHeaderChunks& csrHeader);

private:
std::unique_ptr<CopyRelInfo> info;
std::shared_ptr<PartitionerSharedState> partitionerSharedState;
std::shared_ptr<CopyRelSharedState> sharedState;
Expand Down
27 changes: 0 additions & 27 deletions src/include/processor/operator/scan/scan_rel_csr_columns.h

This file was deleted.

Loading

0 comments on commit f9e9e29

Please sign in to comment.