Skip to content

Commit

Permalink
fix rel updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ray6080 committed Nov 16, 2023
1 parent a616abf commit 6237df3
Show file tree
Hide file tree
Showing 41 changed files with 945 additions and 366 deletions.
1 change: 0 additions & 1 deletion src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ add_subdirectory(vector)

add_library(kuzu_common
OBJECT
rel_direction.cpp
expression_type.cpp
file_utils.cpp
in_mem_overflow_buffer.cpp
Expand Down
22 changes: 0 additions & 22 deletions src/common/rel_direction.cpp

This file was deleted.

10 changes: 0 additions & 10 deletions src/include/common/enums/rel_direction.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
#pragma once

#include <cstdint>
#include <string>
#include <vector>

namespace kuzu {
namespace common {

enum class RelDataDirection : uint8_t { FWD = 0, BWD = 1 };

struct RelDataDirectionUtils {
static inline std::vector<RelDataDirection> getRelDataDirections() {
return std::vector<RelDataDirection>{RelDataDirection::FWD, RelDataDirection::BWD};
}

static std::string relDataDirectionToString(RelDataDirection direction);
};

} // namespace common
} // namespace kuzu
26 changes: 13 additions & 13 deletions src/include/processor/operator/persistent/set_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class RelSetExecutor {

void init(ResultSet* resultSet, ExecutionContext* context);

virtual void set() = 0;
virtual void set(ExecutionContext* context) = 0;

virtual std::unique_ptr<RelSetExecutor> copy() const = 0;

Expand All @@ -118,50 +118,50 @@ class RelSetExecutor {

class SingleLabelRelSetExecutor : public RelSetExecutor {
public:
SingleLabelRelSetExecutor(storage::RelTable* table, common::property_id_t propertyID,
SingleLabelRelSetExecutor(storage::RelTable* table, common::column_id_t columnID,
const DataPos& srcNodeIDPos, const DataPos& dstNodeIDPos, const DataPos& relIDPos,
const DataPos& lhsVectorPos, std::unique_ptr<evaluator::ExpressionEvaluator> evaluator)
: RelSetExecutor{srcNodeIDPos, dstNodeIDPos, relIDPos, lhsVectorPos, std::move(evaluator)},
table{table}, propertyID{propertyID} {}
table{table}, columnID{columnID} {}
SingleLabelRelSetExecutor(const SingleLabelRelSetExecutor& other)
: RelSetExecutor{other.srcNodeIDPos, other.dstNodeIDPos, other.relIDPos, other.lhsVectorPos,
other.evaluator->clone()},
table{other.table}, propertyID{other.propertyID} {}
table{other.table}, columnID{other.columnID} {}

void set() final;
void set(ExecutionContext* context) final;

inline std::unique_ptr<RelSetExecutor> copy() const final {
return std::make_unique<SingleLabelRelSetExecutor>(*this);
}

private:
storage::RelTable* table;
common::property_id_t propertyID;
common::column_id_t columnID;
};

class MultiLabelRelSetExecutor : public RelSetExecutor {
public:
MultiLabelRelSetExecutor(
std::unordered_map<common::table_id_t, std::pair<storage::RelTable*, common::property_id_t>>
tableIDToTableAndPropertyID,
std::unordered_map<common::table_id_t, std::pair<storage::RelTable*, common::column_id_t>>
tableIDToTableAndColumnID,
const DataPos& srcNodeIDPos, const DataPos& dstNodeIDPos, const DataPos& relIDPos,
const DataPos& lhsVectorPos, std::unique_ptr<evaluator::ExpressionEvaluator> evaluator)
: RelSetExecutor{srcNodeIDPos, dstNodeIDPos, relIDPos, lhsVectorPos, std::move(evaluator)},
tableIDToTableAndPropertyID{std::move(tableIDToTableAndPropertyID)} {}
tableIDToTableAndColumnID{std::move(tableIDToTableAndColumnID)} {}
MultiLabelRelSetExecutor(const MultiLabelRelSetExecutor& other)
: RelSetExecutor{other.srcNodeIDPos, other.dstNodeIDPos, other.relIDPos, other.lhsVectorPos,
other.evaluator->clone()},
tableIDToTableAndPropertyID{other.tableIDToTableAndPropertyID} {}
tableIDToTableAndColumnID{other.tableIDToTableAndColumnID} {}

void set() final;
void set(ExecutionContext* context) final;

inline std::unique_ptr<RelSetExecutor> copy() const final {
return std::make_unique<MultiLabelRelSetExecutor>(*this);
}

private:
std::unordered_map<common::table_id_t, std::pair<storage::RelTable*, common::property_id_t>>
tableIDToTableAndPropertyID;
std::unordered_map<common::table_id_t, std::pair<storage::RelTable*, common::column_id_t>>
tableIDToTableAndColumnID;
};

} // namespace processor
Expand Down
64 changes: 64 additions & 0 deletions src/include/storage/local_storage/local_node_table.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#pragma once

#include "local_table.h"

namespace kuzu {
namespace storage {

class LocalNodeNG final : public LocalNodeGroup {

Check warning on line 8 in src/include/storage/local_storage/local_node_table.h

View check run for this annotation

Codecov / codecov/patch

src/include/storage/local_storage/local_node_table.h#L8

Added line #L8 was not covered by tests
public:
LocalNodeNG(std::vector<common::LogicalType*> dataTypes, MemoryManager* mm)
: LocalNodeGroup{dataTypes, mm} {
insertInfo.resize(dataTypes.size());
updateInfo.resize(dataTypes.size());
}

void scan(common::ValueVector* nodeIDVector, const std::vector<common::column_id_t>& columnIDs,
const std::vector<common::ValueVector*>& outputVectors);
void lookup(common::offset_t nodeOffset, common::column_id_t columnID,
common::ValueVector* outputVector, common::sel_t posInOutputVector);
void insert(common::ValueVector* nodeIDVector,
const std::vector<common::ValueVector*>& propertyVectors);
void update(common::ValueVector* nodeIDVector, common::column_id_t columnID,
common::ValueVector* propertyVector);
void delete_(common::ValueVector* nodeIDVector);

common::row_idx_t getRowIdx(common::column_id_t columnID, common::offset_t nodeOffset);

inline const offset_to_row_idx_t& getInsertInfoRef(common::column_id_t columnID) {
KU_ASSERT(columnID < insertInfo.size());
return insertInfo[columnID];
}
inline const offset_to_row_idx_t& getUpdateInfoRef(common::column_id_t columnID) {
KU_ASSERT(columnID < updateInfo.size());
return updateInfo[columnID];
}

private:
std::vector<offset_to_row_idx_t> insertInfo;
std::vector<offset_to_row_idx_t> updateInfo;
};

class LocalNodeTableData final : public LocalTableData {

Check warning on line 42 in src/include/storage/local_storage/local_node_table.h

View check run for this annotation

Codecov / codecov/patch

src/include/storage/local_storage/local_node_table.h#L42

Added line #L42 was not covered by tests
public:
LocalNodeTableData(std::vector<common::LogicalType*> dataTypes, MemoryManager* mm,
common::ColumnDataFormat dataFormat)
: LocalTableData{dataTypes, mm, dataFormat} {}

void scan(common::ValueVector* nodeIDVector, const std::vector<common::column_id_t>& columnIDs,
const std::vector<common::ValueVector*>& outputVectors);
void lookup(common::ValueVector* nodeIDVector,
const std::vector<common::column_id_t>& columnIDs,
const std::vector<common::ValueVector*>& outputVectors);
void insert(common::ValueVector* nodeIDVector,
const std::vector<common::ValueVector*>& propertyVectors);
void update(common::ValueVector* nodeIDVector, common::column_id_t columnID,
common::ValueVector* propertyVector);
void delete_(common::ValueVector* nodeIDVector);

private:
LocalNodeGroup* getOrCreateLocalNodeGroup(common::ValueVector* nodeIDVector);
};

} // namespace storage
} // namespace kuzu
109 changes: 109 additions & 0 deletions src/include/storage/local_storage/local_rel_table.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#pragma once

#include "common/column_data_format.h"
#include "storage/local_storage/local_table.h"

namespace kuzu {
namespace storage {

static constexpr common::column_id_t REL_ID_COLUMN_ID = 0;

struct RelNGInfo {
virtual ~RelNGInfo() = default;

virtual void insert(common::offset_t srcNodeOffset, common::offset_t relOffset,
common::row_idx_t adjNodeRowIdx,
const std::vector<common::row_idx_t>& propertyNodesRowIdx) = 0;
virtual void update(common::offset_t srcNodeOffset, common::offset_t relOffset,
common::column_id_t columnID, common::row_idx_t rowIdx) = 0;
virtual bool delete_(common::offset_t srcNodeOffset, common::offset_t relOffset) = 0;

protected:
inline static bool contains(
const std::unordered_set<common::offset_t>& set, common::offset_t value) {
return set.find(value) != set.end();
}
};

// Info of node groups with regular chunks for rel tables.
struct RegularRelNGInfo final : public RelNGInfo {

Check warning on line 29 in src/include/storage/local_storage/local_rel_table.h

View check run for this annotation

Codecov / codecov/patch

src/include/storage/local_storage/local_rel_table.h#L29

Added line #L29 was not covered by tests
// Note that adj chunk cannot be directly updated. It can only be inserted or deleted.
offset_to_row_idx_t adjInsertInfo; // insert info for adj chunk.
std::vector<offset_to_row_idx_t> insertInfoPerChunk; // insert info for property chunks.
std::vector<offset_to_row_idx_t> updateInfoPerChunk; // insert info for property chunks.
offset_set_t deleteInfo; // the set of deleted node offsets.

RegularRelNGInfo(common::column_id_t numChunks) {
insertInfoPerChunk.resize(numChunks);
updateInfoPerChunk.resize(numChunks);
}

void insert(common::offset_t srcNodeOffset, common::offset_t relOffset,
common::row_idx_t adjNodeRowIdx, const std::vector<common::row_idx_t>& propertyNodesRowIdx);
void update(common::offset_t srcNodeOffset, common::offset_t relOffset,
common::column_id_t columnID, common::row_idx_t rowIdx);
bool delete_(common::offset_t srcNodeOffset, common::offset_t relOffset);
};

// Info of node groups with CSR chunks for rel tables.
struct CSRRelNGInfo final : public RelNGInfo {

Check warning on line 49 in src/include/storage/local_storage/local_rel_table.h

View check run for this annotation

Codecov / codecov/patch

src/include/storage/local_storage/local_rel_table.h#L49

Added line #L49 was not covered by tests
offset_to_offset_to_row_idx_t adjInsertInfo;
std::vector<offset_to_offset_to_row_idx_t> insertInfoPerChunk;
std::vector<offset_to_offset_to_row_idx_t> updateInfoPerChunk;
offset_to_offset_set_t deleteInfo;

CSRRelNGInfo(common::column_id_t numChunks) {
insertInfoPerChunk.resize(numChunks);
updateInfoPerChunk.resize(numChunks);
}

void insert(common::offset_t srcNodeOffset, common::offset_t relOffset,
common::row_idx_t adjNodeRowIdx, const std::vector<common::row_idx_t>& propertyNodesRowIdx);
void update(common::offset_t srcNodeOffset, common::offset_t relOffset,
common::column_id_t columnID, common::row_idx_t rowIdx);
bool delete_(common::offset_t srcNodeOffset, common::offset_t relOffset);
};

class LocalRelNG final : public LocalNodeGroup {

Check warning on line 67 in src/include/storage/local_storage/local_rel_table.h

View check run for this annotation

Codecov / codecov/patch

src/include/storage/local_storage/local_rel_table.h#L67

Added line #L67 was not covered by tests
public:
LocalRelNG(common::ColumnDataFormat dataFormat, std::vector<common::LogicalType*> dataTypes,
MemoryManager* mm);

void insert(common::ValueVector* srcNodeIDVector, common::ValueVector* dstNodeIDVector,
const std::vector<common::ValueVector*>& propertyVectors);
void update(common::ValueVector* srcNodeIDVector, common::ValueVector* relIDVector,
common::column_id_t columnID, common::ValueVector* propertyVector);
void delete_(common::ValueVector* srcNodeIDVector, common::ValueVector* relIDVector);

inline LocalVectorCollection* getAdjChunk() { return adjChunk.get(); }
inline LocalVectorCollection* getPropertyChunk(common::column_id_t columnID) {
KU_ASSERT(columnID < chunks.size());
return chunks[columnID].get();
}
inline RelNGInfo* getRelNGInfo() { return relNGInfo.get(); }

private:
std::unique_ptr<LocalVectorCollection> adjChunk;
std::unique_ptr<RelNGInfo> relNGInfo;
};

class LocalRelTableData final : public LocalTableData {

Check warning on line 90 in src/include/storage/local_storage/local_rel_table.h

View check run for this annotation

Codecov / codecov/patch

src/include/storage/local_storage/local_rel_table.h#L90

Added line #L90 was not covered by tests
friend class RelTableData;

public:
LocalRelTableData(std::vector<common::LogicalType*> dataTypes, MemoryManager* mm,
common::ColumnDataFormat dataFormat)
: LocalTableData{dataTypes, mm, dataFormat} {}

void insert(common::ValueVector* srcNodeIDVector, common::ValueVector* dstNodeIDVector,
const std::vector<common::ValueVector*>& propertyVectors);
void update(common::ValueVector* srcNodeIDVector, common::ValueVector* relIDVector,
common::column_id_t columnID, common::ValueVector* propertyVector);
void delete_(common::ValueVector* srcNodeIDVector, common::ValueVector* relIDVector);

private:
LocalNodeGroup* getOrCreateLocalNodeGroup(common::ValueVector* nodeIDVector);
};

} // namespace storage
} // namespace kuzu
12 changes: 8 additions & 4 deletions src/include/storage/local_storage/local_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ class LocalStorage {
LocalStorage(storage::MemoryManager* mm);

// This function will create the local table data if not exists.
LocalTableData* getOrCreateLocalTableData(
common::table_id_t tableID, const std::vector<std::unique_ptr<Column>>& columns);
// This function will return nullptr if the local table data does not exist.
LocalTableData* getLocalTableData(common::table_id_t tableID);
LocalTableData* getOrCreateLocalTableData(common::table_id_t tableID,
const std::vector<std::unique_ptr<Column>>& columns,
common::TableType tableType = common::TableType::NODE,
common::ColumnDataFormat dataFormat = common::ColumnDataFormat::REGULAR,
common::vector_idx_t dataIdx = 0);
LocalTable* getLocalTable(common::table_id_t tableID);
// This function will return nullptr if the local table does not exist.
LocalTableData* getLocalTableData(common::table_id_t tableID, common::vector_idx_t dataIdx = 0);
std::unordered_set<common::table_id_t> getTableIDsWithUpdates();

private:
Expand Down
Loading

0 comments on commit 6237df3

Please sign in to comment.