Skip to content

Commit

Permalink
add delete rel planning
Browse files Browse the repository at this point in the history
  • Loading branch information
andyfengHKU committed Nov 21, 2022
1 parent ef57ca7 commit ed50628
Show file tree
Hide file tree
Showing 25 changed files with 440 additions and 260 deletions.
25 changes: 17 additions & 8 deletions src/binder/bind/bind_updating_clause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,23 @@ unique_ptr<BoundUpdatingClause> Binder::bindCreateClause(const UpdatingClause& u
auto prevVariablesInScope = variablesInScope;
auto [queryGraphCollection, propertyCollection] =
bindGraphPattern(createClause.getPatternElements());
vector<unique_ptr<BoundCreateNode>> boundCreateNodes;
vector<unique_ptr<BoundCreateRel>> boundCreateRels;
auto boundCreateClause = make_unique<BoundCreateClause>();
for (auto i = 0u; i < queryGraphCollection->getNumQueryGraphs(); ++i) {
auto queryGraph = queryGraphCollection->getQueryGraph(i);
for (auto j = 0u; j < queryGraph->getNumQueryNodes(); ++j) {
auto node = queryGraph->getQueryNode(j);
if (!prevVariablesInScope.contains(node->getRawName())) {
boundCreateNodes.push_back(bindCreateNode(node, *propertyCollection));
boundCreateClause->addCreateNode(bindCreateNode(node, *propertyCollection));
}
}
for (auto j = 0u; j < queryGraph->getNumQueryRels(); ++j) {
auto rel = queryGraph->getQueryRel(j);
if (!prevVariablesInScope.contains(rel->getRawName())) {
boundCreateRels.push_back(bindCreateRel(rel, *propertyCollection));
boundCreateClause->addCreateRel(bindCreateRel(rel, *propertyCollection));
}
}
}
auto boundCreateClause =
make_unique<BoundCreateClause>(std::move(boundCreateNodes), std::move(boundCreateRels));

return boundCreateClause;
}

Expand Down Expand Up @@ -117,15 +115,26 @@ unique_ptr<BoundUpdatingClause> Binder::bindDeleteClause(const UpdatingClause& u
auto boundDeleteClause = make_unique<BoundDeleteClause>();
for (auto i = 0u; i < deleteClause.getNumExpressions(); ++i) {
auto boundExpression = expressionBinder.bindExpression(*deleteClause.getExpression(i));
if (boundExpression->dataType.typeID != NODE) {
if (boundExpression->dataType.typeID == NODE) {
boundDeleteClause->addDeleteNode(
bindDeleteNode(static_pointer_cast<NodeExpression>(boundExpression)));
} else if (boundExpression->dataType.typeID == REL) {
boundDeleteClause->addDeleteRel(static_pointer_cast<RelExpression>(boundExpression));
} else {
throw BinderException("Delete " +
expressionTypeToString(boundExpression->expressionType) +
" is not supported.");
}
boundDeleteClause->addExpression(move(boundExpression));
}
return boundDeleteClause;
}

unique_ptr<BoundDeleteNode> Binder::bindDeleteNode(shared_ptr<NodeExpression> node) {
auto nodeTableSchema = catalog.getReadOnlyVersion()->getNodeTableSchema(node->getTableID());
auto primaryKey = nodeTableSchema->getPrimaryKey();
auto primaryKeyExpression = expressionBinder.bindNodePropertyExpression(node, primaryKey);
return make_unique<BoundDeleteNode>(node, primaryKeyExpression);
}

} // namespace binder
} // namespace kuzu
2 changes: 2 additions & 0 deletions src/binder/include/binder.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace binder {

class BoundCreateNode;
class BoundCreateRel;
class BoundDeleteNode;

class Binder {
friend class ExpressionBinder;
Expand Down Expand Up @@ -76,6 +77,7 @@ class Binder {
shared_ptr<NodeExpression> node, const PropertyKeyValCollection& collection);
unique_ptr<BoundCreateRel> bindCreateRel(
shared_ptr<RelExpression> rel, const PropertyKeyValCollection& collection);
unique_ptr<BoundDeleteNode> bindDeleteNode(shared_ptr<NodeExpression> node);

/*** bind projection clause ***/
unique_ptr<BoundWithClause> bindWithClause(const WithClause& withClause);
Expand Down
10 changes: 4 additions & 6 deletions src/binder/query/updating_clause/bound_create_clause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,14 @@ expression_vector BoundCreateClause::getPropertiesToRead() const {
}

unique_ptr<BoundUpdatingClause> BoundCreateClause::copy() {
vector<unique_ptr<BoundCreateNode>> copiedCreateNodes;
vector<unique_ptr<BoundCreateRel>> copiedCreateRels;
auto result = make_unique<BoundCreateClause>();
for (auto& createNode : createNodes) {
copiedCreateNodes.push_back(createNode->copy());
result->addCreateNode(createNode->copy());
}
for (auto& createRel : createRels) {
copiedCreateRels.push_back(createRel->copy());
result->addCreateRel(createRel->copy());
}
return make_unique<BoundCreateClause>(
std::move(copiedCreateNodes), std::move(copiedCreateRels));
return result;
}

} // namespace binder
Expand Down
26 changes: 26 additions & 0 deletions src/binder/query/updating_clause/bound_delete_clause.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "include/bound_delete_clause.h"

namespace kuzu {
namespace binder {

expression_vector BoundDeleteClause::getPropertiesToRead() const {
expression_vector result;
for (auto& deleteNode : deleteNodes) {
result.push_back(deleteNode->getPrimaryKeyExpression());
}
return result;
}

unique_ptr<BoundUpdatingClause> BoundDeleteClause::copy() {
auto result = make_unique<BoundDeleteClause>();
for (auto& deleteNode : deleteNodes) {
result->addDeleteNode(deleteNode->copy());
}
for (auto& deleteRel : deleteRels) {
result->addDeleteRel(deleteRel);
}
return result;
}

} // namespace binder
} // namespace kuzu
13 changes: 9 additions & 4 deletions src/binder/query/updating_clause/include/bound_create_clause.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,24 @@ class BoundCreateRel : public BoundCreateNodeOrRel {

class BoundCreateClause : public BoundUpdatingClause {
public:
BoundCreateClause(vector<unique_ptr<BoundCreateNode>> createNodes,
vector<unique_ptr<BoundCreateRel>> createRels)
: BoundUpdatingClause{ClauseType::CREATE}, createNodes{std::move(createNodes)},
createRels{std::move(createRels)} {};
BoundCreateClause() : BoundUpdatingClause{ClauseType::CREATE} {};
~BoundCreateClause() override = default;

inline void addCreateNode(unique_ptr<BoundCreateNode> createNode) {
createNodes.push_back(std::move(createNode));
}
inline bool hasCreateNode() const { return !createNodes.empty(); }
inline uint32_t getNumCreateNodes() const { return createNodes.size(); }
inline BoundCreateNode* getCreateNode(uint32_t idx) const { return createNodes[idx].get(); }
inline const vector<unique_ptr<BoundCreateNode>>& getCreateNodes() const { return createNodes; }

inline void addCreateRel(unique_ptr<BoundCreateRel> createRel) {
createRels.push_back(std::move(createRel));
}
inline bool hasCreateRel() const { return !createRels.empty(); }
inline uint32_t getNumCreateRels() const { return createRels.size(); }
inline BoundCreateRel* getCreateRel(uint32_t idx) const { return createRels[idx].get(); }
inline const vector<unique_ptr<BoundCreateRel>>& getCreateRels() const { return createRels; }

vector<expression_pair> getAllSetItems() const;
expression_vector getPropertiesToRead() const override;
Expand Down
58 changes: 37 additions & 21 deletions src/binder/query/updating_clause/include/bound_delete_clause.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,56 @@

#include "bound_updating_clause.h"

#include "src/binder/expression/include/rel_expression.h"

namespace kuzu {
namespace binder {

class BoundDeleteNode {
public:
BoundDeleteNode(shared_ptr<NodeExpression> node, shared_ptr<Expression> primaryKeyExpression)
: node{std::move(node)}, primaryKeyExpression{std::move(primaryKeyExpression)} {}

inline shared_ptr<NodeExpression> getNode() const { return node; }
inline shared_ptr<Expression> getPrimaryKeyExpression() const { return primaryKeyExpression; }

inline unique_ptr<BoundDeleteNode> copy() {
return make_unique<BoundDeleteNode>(node, primaryKeyExpression);
}

private:
shared_ptr<NodeExpression> node;
shared_ptr<Expression> primaryKeyExpression;
};

class BoundDeleteClause : public BoundUpdatingClause {
public:
BoundDeleteClause() : BoundUpdatingClause{ClauseType::DELETE} {};
~BoundDeleteClause() override = default;

inline void addExpression(shared_ptr<Expression> expression) {
expressions.push_back(move(expression));
}
inline uint32_t getNumExpressions() const { return expressions.size(); }
inline shared_ptr<Expression> getExpression(uint32_t idx) const { return expressions[idx]; }

inline expression_vector getPropertiesToRead() const override {
expression_vector result;
for (auto& expression : expressions) {
for (auto& property : expression->getSubPropertyExpressions()) {
result.push_back(property);
}
}
return result;
inline void addDeleteNode(unique_ptr<BoundDeleteNode> deleteNode) {
deleteNodes.push_back(std::move(deleteNode));
}
inline bool hasDeleteNode() const { return !deleteNodes.empty(); }
inline uint32_t getNumDeleteNodes() const { return deleteNodes.size(); }
inline BoundDeleteNode* getDeleteNode(uint32_t idx) const { return deleteNodes[idx].get(); }
inline const vector<unique_ptr<BoundDeleteNode>>& getDeleteNodes() const { return deleteNodes; }

inline unique_ptr<BoundUpdatingClause> copy() override {
auto result = make_unique<BoundDeleteClause>();
for (auto& expression : expressions) {
result->addExpression(expression);
}
return result;
inline void addDeleteRel(shared_ptr<RelExpression> deleteRel) {
deleteRels.push_back(std::move(deleteRel));
}
inline bool hasDeleteRel() const { return !deleteRels.empty(); }
inline uint32_t getNumDeleteRels() const { return deleteRels.size(); }
inline shared_ptr<RelExpression> getDeleteRel(uint32_t idx) const { return deleteRels[idx]; }
inline vector<shared_ptr<RelExpression>> getDeleteRels() const { return deleteRels; }

expression_vector getPropertiesToRead() const override;

unique_ptr<BoundUpdatingClause> copy() override;

private:
vector<shared_ptr<Expression>> expressions;
vector<unique_ptr<BoundDeleteNode>> deleteNodes;
vector<shared_ptr<RelExpression>> deleteRels;
};

} // namespace binder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct ArrayExtract {
auto startPos = idxPos - 1;
auto endPos = startPos + 1;
bool isAscii = true;
for (auto i = 0u; i < min((uint64_t)idxPos + 1, stringVal.size()); i++) {
for (auto i = 0u; i < min((size_t)idxPos + 1, stringVal.size()); i++) {
if (stringVal[i] & 0x80) {
isAscii = false;
break;
Expand Down
8 changes: 3 additions & 5 deletions src/planner/include/query_planner.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ class QueryPlanner {

public:
explicit QueryPlanner(const Catalog& catalog,
const NodesStatisticsAndDeletedIDs& nodesStatisticsAndDeletedIDs,
const RelsStatistics& relsStatistics)
: catalog{catalog}, joinOrderEnumerator{catalog, nodesStatisticsAndDeletedIDs,
relsStatistics, this},
projectionPlanner{this}, updatePlanner{catalog, this} {}
const NodesStatisticsAndDeletedIDs& nodesStatistics, const RelsStatistics& relsStatistics)
: catalog{catalog}, joinOrderEnumerator{catalog, nodesStatistics, relsStatistics, this},
projectionPlanner{this}, updatePlanner{} {}

vector<unique_ptr<LogicalPlan>> getAllPlans(const BoundStatement& boundStatement);

Expand Down
23 changes: 8 additions & 15 deletions src/planner/include/update_planner.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,14 @@
#include "src/binder/query/updating_clause/include/bound_delete_clause.h"
#include "src/binder/query/updating_clause/include/bound_set_clause.h"
#include "src/binder/query/updating_clause/include/bound_updating_clause.h"
#include "src/catalog/include/catalog.h"
#include "src/planner/logical_plan/include/logical_plan.h"

namespace kuzu {
namespace planner {

using namespace kuzu::catalog;

class QueryPlanner;

class UpdatePlanner {
public:
UpdatePlanner(const catalog::Catalog& catalog, QueryPlanner* queryPlanner)
: catalog{catalog}, queryPlanner{queryPlanner} {};
UpdatePlanner() = default;

inline void planUpdatingClause(
BoundUpdatingClause& updatingClause, vector<unique_ptr<LogicalPlan>>& plans) {
Expand All @@ -31,20 +25,19 @@ class UpdatePlanner {
void planSetItem(expression_pair setItem, LogicalPlan& plan);

void planCreate(BoundCreateClause& createClause, LogicalPlan& plan);
void appendCreateNode(BoundCreateClause& createClause, LogicalPlan& plan);
void appendCreateRel(BoundCreateClause& createClause, LogicalPlan& plan);
void appendCreateNode(
const vector<unique_ptr<BoundCreateNode>>& createNodes, LogicalPlan& plan);
void appendCreateRel(const vector<unique_ptr<BoundCreateRel>>& createRels, LogicalPlan& plan);

inline void appendSet(BoundSetClause& setClause, LogicalPlan& plan) {
appendSet(setClause.getSetItems(), plan);
}
void appendSet(vector<expression_pair> setItems, LogicalPlan& plan);
void appendDelete(BoundDeleteClause& deleteClause, LogicalPlan& plan);

vector<expression_pair> splitSetItems(vector<expression_pair> setItems);

private:
const Catalog& catalog;
QueryPlanner* queryPlanner;
void planDelete(BoundDeleteClause& deleteClause, LogicalPlan& plan);
void appendDeleteNode(
const vector<unique_ptr<BoundDeleteNode>>& deleteNodes, LogicalPlan& plan);
void appendDeleteRel(const vector<shared_ptr<RelExpression>>& deleteRels, LogicalPlan& plan);
};

} // namespace planner
Expand Down
8 changes: 4 additions & 4 deletions src/planner/logical_plan/include/logical_plan.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class LogicalPlan {
inline bool isEmpty() const { return lastOperator == nullptr; }

inline bool isReadOnly() const {
return !lastOperator->descendantsContainType(
unordered_set<LogicalOperatorType>{LOGICAL_SET_NODE_PROPERTY, LOGICAL_CREATE_NODE,
LOGICAL_CREATE_REL, LOGICAL_DELETE, LOGICAL_CREATE_NODE_TABLE,
LOGICAL_CREATE_REL_TABLE, LOGICAL_COPY_CSV, LOGICAL_DROP_TABLE});
return !lastOperator->descendantsContainType(unordered_set<LogicalOperatorType>{
LOGICAL_SET_NODE_PROPERTY, LOGICAL_CREATE_NODE, LOGICAL_CREATE_REL, LOGICAL_DELETE_NODE,
LOGICAL_DELETE_REL, LOGICAL_CREATE_NODE_TABLE, LOGICAL_CREATE_REL_TABLE,
LOGICAL_COPY_CSV, LOGICAL_DROP_TABLE});
}

inline void setExpressionsToCollect(expression_vector expressions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ enum LogicalOperatorType : uint8_t {
LOGICAL_CREATE_NODE,
LOGICAL_CREATE_REL,
LOGICAL_SET_NODE_PROPERTY,
LOGICAL_DELETE,
LOGICAL_DELETE_NODE,
LOGICAL_DELETE_REL,
LOGICAL_ACCUMULATE,
LOGICAL_EXPRESSIONS_SCAN,
LOGICAL_FTABLE_SCAN,
Expand All @@ -47,9 +48,10 @@ const string LogicalOperatorTypeNames[] = {"LOGICAL_SCAN_NODE", "LOGICAL_INDEX_S
"LOGICAL_CROSS_PRODUCT", "LOGICAL_SEMI_MASKER", "LOGICAL_HASH_JOIN",
"LOGICAL_MULTIPLICITY_REDUCER", "LOGICAL_LIMIT", "LOGICAL_SKIP", "LOGICAL_AGGREGATE",
"LOGICAL_ORDER_BY", "LOGICAL_UNION_ALL", "LOGICAL_DISTINCT", "LOGICAL_CREATE_NODE",
"LOGICAL_CREATE_REL", "LOGICAL_SET_NODE_PROPERTY", "LOGICAL_DELETE", "LOGICAL_ACCUMULATE",
"LOGICAL_EXPRESSIONS_SCAN", "LOGICAL_FTABLE_SCAN", "LOGICAL_CREATE_NODE_TABLE",
"LOGICAL_CREATE_REL_TABLE", "LOGICAL_COPY_CSV", "LOGICAL_DROP_TABLE"};
"LOGICAL_CREATE_REL", "LOGICAL_SET_NODE_PROPERTY", "LOGICAL_DELETE_NODE", "LOGICAL_DELETE_REL",
"LOGICAL_ACCUMULATE", "LOGICAL_EXPRESSIONS_SCAN", "LOGICAL_FTABLE_SCAN",
"LOGICAL_CREATE_NODE_TABLE", "LOGICAL_CREATE_REL_TABLE", "LOGICAL_COPY_CSV",
"LOGICAL_DROP_TABLE"};

class LogicalOperator {
public:
Expand Down
Loading

0 comments on commit ed50628

Please sign in to comment.