Skip to content

Commit

Permalink
clean
Browse files Browse the repository at this point in the history
  • Loading branch information
andyfengHKU committed Aug 2, 2023
1 parent b725f99 commit 2c38cc8
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 51 deletions.
47 changes: 24 additions & 23 deletions src/include/processor/operator/update/set_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace processor {

class NodeSetExecutor {
public:
NodeSetExecutor(
const DataPos& nodeIDPos, const DataPos& propertyPos, std::unique_ptr<evaluator::BaseExpressionEvaluator> evaluator)
NodeSetExecutor(const DataPos& nodeIDPos, const DataPos& propertyPos,
std::unique_ptr<evaluator::BaseExpressionEvaluator> evaluator)
: nodeIDPos{nodeIDPos}, propertyPos{propertyPos}, evaluator{std::move(evaluator)} {}
virtual ~NodeSetExecutor() = default;

Expand All @@ -22,9 +22,6 @@ class NodeSetExecutor {

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

protected:
void writeToPropertyVector(uint32_t lhsPos, uint32_t rhsPos);

protected:
DataPos nodeIDPos;
DataPos propertyPos;
Expand All @@ -37,11 +34,12 @@ class NodeSetExecutor {

class SingleLabelNodeSetExecutor : public NodeSetExecutor {
public:
SingleLabelNodeSetExecutor(storage::Column* column, const DataPos& nodeIDPos, const DataPos& propertyPos,
std::unique_ptr<evaluator::BaseExpressionEvaluator> evaluator)
SingleLabelNodeSetExecutor(storage::Column* column, const DataPos& nodeIDPos,
const DataPos& propertyPos, std::unique_ptr<evaluator::BaseExpressionEvaluator> evaluator)
: NodeSetExecutor{nodeIDPos, propertyPos, std::move(evaluator)}, column{column} {}
SingleLabelNodeSetExecutor(const SingleLabelNodeSetExecutor& other)
: NodeSetExecutor{other.nodeIDPos, other.propertyPos, other.evaluator->clone()}, column{other.column} {}
: NodeSetExecutor{other.nodeIDPos, other.propertyPos, other.evaluator->clone()},
column{other.column} {}

void set() final;

Expand All @@ -57,12 +55,13 @@ class MultiLabelNodeSetExecutor : public NodeSetExecutor {
public:
MultiLabelNodeSetExecutor(
std::unordered_map<common::table_id_t, storage::Column*> tableIDToColumn,
const DataPos& nodeIDPos,const DataPos& propertyPos, std::unique_ptr<evaluator::BaseExpressionEvaluator> evaluator)
: NodeSetExecutor{nodeIDPos, propertyPos, std::move(evaluator)}, tableIDToColumn{
std::move(tableIDToColumn)} {}
const DataPos& nodeIDPos, const DataPos& propertyPos,
std::unique_ptr<evaluator::BaseExpressionEvaluator> evaluator)
: NodeSetExecutor{nodeIDPos, propertyPos, std::move(evaluator)}, tableIDToColumn{std::move(
tableIDToColumn)} {}
MultiLabelNodeSetExecutor(const MultiLabelNodeSetExecutor& other)
: NodeSetExecutor{other.nodeIDPos, other.propertyPos, other.evaluator->clone()}, tableIDToColumn{
other.tableIDToColumn} {}
: NodeSetExecutor{other.nodeIDPos, other.propertyPos, other.evaluator->clone()},
tableIDToColumn{other.tableIDToColumn} {}

void set() final;

Expand All @@ -77,9 +76,10 @@ class MultiLabelNodeSetExecutor : public NodeSetExecutor {
class RelSetExecutor {
public:
RelSetExecutor(const DataPos& srcNodeIDPos, const DataPos& dstNodeIDPos,
const DataPos& relIDPos, std::unique_ptr<evaluator::BaseExpressionEvaluator> evaluator)
: srcNodeIDPos{srcNodeIDPos},
dstNodeIDPos{dstNodeIDPos}, relIDPos{relIDPos}, evaluator{std::move(evaluator)} {}
const DataPos& relIDPos, const DataPos& propertyPos,
std::unique_ptr<evaluator::BaseExpressionEvaluator> evaluator)
: srcNodeIDPos{srcNodeIDPos}, dstNodeIDPos{dstNodeIDPos}, relIDPos{relIDPos},
propertyPos{propertyPos}, evaluator{std::move(evaluator)} {}
virtual ~RelSetExecutor() = default;

void init(ResultSet* resultSet, ExecutionContext* context);
Expand All @@ -98,18 +98,19 @@ class RelSetExecutor {
common::ValueVector* srcNodeIDVector;
common::ValueVector* dstNodeIDVector;
common::ValueVector* relIDVector;
common::ValueVector* propertyVector;
common::ValueVector* rhsVector;
};

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

Expand All @@ -130,11 +131,11 @@ class MultiLabelRelSetExecutor : public RelSetExecutor {
std::unordered_map<common::table_id_t, std::pair<storage::RelTable*, common::property_id_t>>
tableIDToTableAndPropertyID,
const DataPos& srcNodeIDPos, const DataPos& dstNodeIDPos, const DataPos& relIDPos,
std::unique_ptr<evaluator::BaseExpressionEvaluator> evaluator)
: RelSetExecutor{srcNodeIDPos, dstNodeIDPos, relIDPos, std::move(evaluator)},
const DataPos& propertyPos, std::unique_ptr<evaluator::BaseExpressionEvaluator> evaluator)
: RelSetExecutor{srcNodeIDPos, dstNodeIDPos, relIDPos, propertyPos, std::move(evaluator)},
tableIDToTableAndPropertyID{std::move(tableIDToTableAndPropertyID)} {}
MultiLabelRelSetExecutor(const MultiLabelRelSetExecutor& other)
: RelSetExecutor{other.srcNodeIDPos, other.dstNodeIDPos, other.relIDPos,
: RelSetExecutor{other.srcNodeIDPos, other.dstNodeIDPos, other.relIDPos, other.propertyPos,
other.evaluator->clone()},
tableIDToTableAndPropertyID{other.tableIDToTableAndPropertyID} {}

Expand Down
10 changes: 7 additions & 3 deletions src/processor/mapper/map_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,16 @@ static std::unique_ptr<RelSetExecutor> getRelSetExecutor(storage::RelsStore* sto
const LogicalSetPropertyInfo& info, const Schema& inSchema,
std::unique_ptr<BaseExpressionEvaluator> evaluator) {
auto rel = (RelExpression*)info.nodeOrRel.get();
auto property = (PropertyExpression*)info.setItem.first.get();
auto srcNodePos =
DataPos(inSchema.getExpressionPos(*rel->getSrcNode()->getInternalIDProperty()));
auto dstNodePos =
DataPos(inSchema.getExpressionPos(*rel->getDstNode()->getInternalIDProperty()));
auto relIDPos = DataPos(inSchema.getExpressionPos(*rel->getInternalIDProperty()));
auto property = (PropertyExpression*)info.setItem.first.get();
auto propertyPos = DataPos(INVALID_DATA_CHUNK_POS, INVALID_VALUE_VECTOR_POS);
if (inSchema.isExpressionInScope(*property)) {
propertyPos = DataPos(inSchema.getExpressionPos(*property));
}
if (rel->isMultiLabeled()) {
std::unordered_map<common::table_id_t, std::pair<storage::RelTable*, common::property_id_t>>
tableIDToTableAndPropertyID;
Expand All @@ -76,13 +80,13 @@ static std::unique_ptr<RelSetExecutor> getRelSetExecutor(storage::RelsStore* sto
tableIDToTableAndPropertyID.insert({tableID, std::make_pair(table, propertyID)});
}
return std::make_unique<MultiLabelRelSetExecutor>(std::move(tableIDToTableAndPropertyID),
srcNodePos, dstNodePos, relIDPos, std::move(evaluator));
srcNodePos, dstNodePos, relIDPos, propertyPos, std::move(evaluator));
} else {
auto tableID = rel->getSingleTableID();
auto table = store->getRelTable(tableID);
auto propertyID = property->getPropertyID(tableID);
return std::make_unique<SingleLabelRelSetExecutor>(
table, propertyID, srcNodePos, dstNodePos, relIDPos, std::move(evaluator));
table, propertyID, srcNodePos, dstNodePos, relIDPos, propertyPos, std::move(evaluator));
}
}

Expand Down
51 changes: 41 additions & 10 deletions src/processor/operator/update/set_executor.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "processor/operator/update/set_executor.h"

using namespace kuzu::common;

namespace kuzu {
namespace processor {

Expand All @@ -14,15 +16,14 @@ void NodeSetExecutor::init(ResultSet* resultSet, ExecutionContext* context) {
rhsVector = evaluator->resultVector.get();
}

void NodeSetExecutor::writeToPropertyVector(uint32_t lhsPos, uint32_t rhsPos) {
if (propertyVector == nullptr) {
return;
}
if (rhsVector->isNull(rhsPos)) {
propertyVector->setNull(lhsPos, true);
return;
static void writeToPropertyVector(ValueVector* propertyVector, uint32_t propertyVectorPos,
ValueVector* rhsVector, uint32_t rhsVectorPos) {
if (rhsVector->isNull(rhsVectorPos)) {
propertyVector->setNull(propertyVectorPos, true);
} else {
propertyVector->setNull(propertyVectorPos, false);
propertyVector->copyFromVectorData(propertyVectorPos, rhsVector, rhsVectorPos);
}
propertyVector->copyFromVectorData(lhsPos, rhsVector, rhsPos);
}

void SingleLabelNodeSetExecutor::set() {
Expand All @@ -34,8 +35,10 @@ void SingleLabelNodeSetExecutor::set() {
if (rhsVector->state->selVector->selectedSize == 1) {
rhsPos = 0;
}
writeToPropertyVector(lhsPos, rhsPos);
column->write(nodeID.offset, rhsVector, rhsPos);
if (propertyVector != nullptr) {
writeToPropertyVector(propertyVector, lhsPos, rhsVector, rhsPos);
}
}
}

Expand All @@ -45,29 +48,51 @@ void MultiLabelNodeSetExecutor::set() {
auto lhsPos = nodeIDVector->state->selVector->selectedPositions[i];
auto& nodeID = nodeIDVector->getValue<common::internalID_t>(lhsPos);
if (!tableIDToColumn.contains(nodeID.tableID)) {
if (propertyVector != nullptr) {
propertyVector->setNull(lhsPos, true);
}
continue;
}
auto column = tableIDToColumn.at(nodeID.tableID);
auto rhsPos = lhsPos;
if (rhsVector->state->selVector->selectedSize == 1) {
rhsPos = 0;
}
writeToPropertyVector(lhsPos, rhsPos);
column->write(nodeID.offset, rhsVector, rhsPos);
if (propertyVector != nullptr) {
writeToPropertyVector(propertyVector, lhsPos, rhsVector, rhsPos);
}
}
}

void RelSetExecutor::init(ResultSet* resultSet, ExecutionContext* context) {
srcNodeIDVector = resultSet->getValueVector(srcNodeIDPos).get();
dstNodeIDVector = resultSet->getValueVector(dstNodeIDPos).get();
relIDVector = resultSet->getValueVector(relIDPos).get();
if (propertyPos.dataChunkPos != INVALID_DATA_CHUNK_POS) {
propertyVector = resultSet->getValueVector(propertyPos).get();
} else {
propertyVector = nullptr;
}
evaluator->init(*resultSet, context->memoryManager);
rhsVector = evaluator->resultVector.get();
}

// Assume both input vectors are flat. Should be removed eventually.
static void writeToPropertyVector(ValueVector* propertyVector, ValueVector* rhsVector) {
assert(propertyVector->state->selVector->selectedSize == 1);
auto propertyVectorPos = propertyVector->state->selVector->selectedPositions[0];
assert(rhsVector->state->selVector->selectedSize == 1);
auto rhsVectorPos = rhsVector->state->selVector->selectedPositions[0];
writeToPropertyVector(propertyVector, propertyVectorPos, rhsVector, rhsVectorPos);
}

void SingleLabelRelSetExecutor::set() {
evaluator->evaluate();
table->updateRel(srcNodeIDVector, dstNodeIDVector, relIDVector, rhsVector, propertyID);
if (propertyVector != nullptr) {
writeToPropertyVector(propertyVector, rhsVector);
}
}

void MultiLabelRelSetExecutor::set() {
Expand All @@ -76,10 +101,16 @@ void MultiLabelRelSetExecutor::set() {
auto pos = relIDVector->state->selVector->selectedPositions[0];
auto relID = relIDVector->getValue<common::internalID_t>(pos);
if (!tableIDToTableAndPropertyID.contains(relID.tableID)) {
if (propertyVector != nullptr) {
propertyVector->setNull(pos, true);
}
return;
}
auto [table, propertyID] = tableIDToTableAndPropertyID.at(relID.tableID);
table->updateRel(srcNodeIDVector, dstNodeIDVector, relIDVector, rhsVector, propertyID);
if (propertyVector != nullptr) {
writeToPropertyVector(propertyVector, rhsVector);
}
}

} // namespace processor
Expand Down
32 changes: 18 additions & 14 deletions test/test_files/tinysnb/update_node/set_read.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
--

-CASE SetReadTest1
-SKIP
-STATEMENT MATCH (a:person) WHERE a.ID=0 RETURN a.age, a.fName
---- 1
35|Alice
Expand All @@ -14,6 +13,12 @@
-STATEMENT MATCH (a:person) WHERE a.ID=0 RETURN a.age, a.gender, a.fName
---- 1
70|1|ALICE
-STATEMENT MATCH (a:person) WHERE a.ID=0 SET a.fName = NULL RETURN a.age, a.gender, a.fName
---- 1
70|1|
-STATEMENT MATCH (a:person) WHERE a.ID=0 RETURN a.age, a.gender, a.fName
---- 1
70|1|
-STATEMENT MATCH (a:person) WHERE a.ID=0 SET a.age = 20 WITH a.age AS AGE MATCH (b:person) WHERE b.age = AGE RETURN AGE, b.fName
---- 2
20|Dan
Expand All @@ -24,21 +29,20 @@
---- ok
-STATEMENT CREATE (a:play {ID: 0, name: 'AA'});
---- ok
#-STATEMENT MATCH (a:organisation:play) SET a.name = 'X' RETURN a.ID, a.name;
#---- 4
#0|X
#1|X
#4|X
#6|X
#-STATEMENT MATCH (a:organisation:play) RETURN a.ID, a.name;
#---- 4
#0|X
#1|X
#4|X
#6|X
-STATEMENT MATCH (a:organisation:play) SET a.name = 'X' RETURN a.ID, a.name;
---- 4
0|X
1|X
4|X
6|X
-STATEMENT MATCH (a:organisation:play) RETURN a.ID, a.name;
---- 4
0|X
1|X
4|X
6|X

-CASE SetReadTest3
-SKIP
-STATEMENT MATCH (a:person)-[e:knows]->(b:person) WHERE a.ID=5 RETURN a.fName, b.fName, e.date
---- 3
Dan|Alice|2021-06-30
Expand Down
38 changes: 37 additions & 1 deletion test/test_files/tinysnb/update_rel/set_read.test
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
-GROUP TinySnbUpdateRelTest
-DATASET CSV tinysnb

--
--

-CASE SetReadTest1
-STATEMENT MATCH (a:person)-[e:knows]->(b:person) WHERE a.ID=0 AND b.ID=5 RETURN e.date;
---- 1
2021-06-30
-STATEMENT MATCH (a:person)-[e:knows]->(b:person) WHERE a.ID=0 AND b.ID=5 SET e.date=date('2023-01-01') RETURN e.date;
---- 1
2023-01-01
-STATEMENT MATCH (a:person)-[e:knows]->(b:person) WHERE a.ID=0 AND b.ID=5 RETURN e.date;
---- 1
2023-01-01
-STATEMENT MATCH (a:person)-[e:knows]->(b:person) WHERE a.ID=0 AND b.ID=5 SET e.date=NULL RETURN e.date;
---- 1

-STATEMENT MATCH (a:person)-[e:knows]->(b:person) WHERE a.ID=0 AND b.ID=5 RETURN e.date;
---- 1

-CASE SetReadTest2
-STATEMENT CREATE REL TABLE play(FROM person TO person, date DATE, year INT64);
---- ok
-STATEMENT MATCH (a:person), (b:person) WHERE a.ID=0 AND b.ID = 2 CREATE (a)-[e:play {date:date('2023-01-01'), year:2023}]->(b);
---- ok
-STATEMENT MATCH (a)-[e]->(b) WHERE a.ID = 0 AND b.ID=2 RETURN id(e), e.date, e.year;
---- 4
3:0|2021-06-30|
6:0||
7:0||
8:0|2023-01-01|2023
-STATEMENT MATCH (a:person)-[e:knows|:play]->(b:person) WHERE a.ID = 0 AND b.ID = 2 SET e.date=date('2023-10-10'), e.year=2024 RETURN e.date, e.year;
---- 2
2023-10-10|
2023-10-10|2024
-STATEMENT MATCH (a:person)-[e:knows|:play]->(b:person) WHERE a.ID = 0 AND b.ID = 2 RETURN e.date, e.year;
---- 2
2023-10-10|
2023-10-10|2024

0 comments on commit 2c38cc8

Please sign in to comment.