Skip to content

Commit

Permalink
Add bound statement visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
andyfengHKU committed May 21, 2023
1 parent cfae96a commit 80e8fb0
Show file tree
Hide file tree
Showing 32 changed files with 295 additions and 176 deletions.
2 changes: 2 additions & 0 deletions src/binder/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ add_subdirectory(bind)
add_subdirectory(bind_expression)
add_subdirectory(expression)
add_subdirectory(query)
add_subdirectory(visitor)

add_library(kuzu_binder
OBJECT
binder.cpp
bound_statement_result.cpp
bound_statement_visitor.cpp
expression_binder.cpp
query_normalizer.cpp)

Expand Down
4 changes: 2 additions & 2 deletions src/binder/binder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ namespace binder {

std::unique_ptr<BoundStatement> Binder::bind(const Statement& statement) {
switch (statement.getStatementType()) {
case StatementType::CREATE_NODE_TABLE_CLAUSE: {
case StatementType::CREATE_NODE_TABLE: {
return bindCreateNodeTableClause(statement);
}
case StatementType::CREATE_REL_TABLE_CLAUSE: {
case StatementType::CREATE_REL_TABLE: {
return bindCreateRelTableClause(statement);
}
case StatementType::COPY: {
Expand Down
99 changes: 99 additions & 0 deletions src/binder/bound_statement_visitor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include "binder/bound_statement_visitor.h"

using namespace kuzu::common;

namespace kuzu {
namespace binder {

void BoundStatementVisitor::visit(const kuzu::binder::BoundStatement& statement) {
switch (statement.getStatementType()) {
case StatementType::QUERY: {
visitRegularQuery((BoundRegularQuery&)statement);
} break;
case StatementType::CREATE_NODE_TABLE: {
visitCreateNodeTable(statement);
} break;
case StatementType::CREATE_REL_TABLE: {
visitCreateRelTable(statement);
} break;
case StatementType::DROP_TABLE: {
visitDropTable(statement);
} break;
case StatementType::RENAME_TABLE: {
visitRenameTable(statement);
} break;
case StatementType::ADD_PROPERTY: {
visitAddProperty(statement);
} break;
case StatementType::DROP_PROPERTY: {
visitDropProperty(statement);
} break;
case StatementType::RENAME_PROPERTY: {
visitRenameProperty(statement);
} break;
case StatementType::COPY: {
visitCopy(statement);
} break;
default:
throw NotImplementedException("BoundStatementVisitor::visit");
}
}

void BoundStatementVisitor::visitRegularQuery(const BoundRegularQuery& regularQuery) {
for (auto i = 0u; i < regularQuery.getNumSingleQueries(); ++i) {
visitSingleQuery(*regularQuery.getSingleQuery(i));
}
}

void BoundStatementVisitor::visitSingleQuery(const NormalizedSingleQuery& singleQuery) {
for (auto i = 0u; i < singleQuery.getNumQueryParts(); ++i) {
visitQueryPart(*singleQuery.getQueryPart(i));
}
}

void BoundStatementVisitor::visitQueryPart(const NormalizedQueryPart& queryPart) {
for (auto i = 0u; i < queryPart.getNumReadingClause(); ++i) {
visitReadingClause(*queryPart.getReadingClause(i));
}
for (auto i = 0u; i < queryPart.getNumUpdatingClause(); ++i) {
visitUpdatingClause(*queryPart.getUpdatingClause(i));
}
if (queryPart.hasProjectionBody()) {
visitProjectionBody(*queryPart.getProjectionBody());
if (queryPart.hasProjectionBodyPredicate()) {
visitProjectionBodyPredicate(*queryPart.getProjectionBodyPredicate());
}
}
}

void BoundStatementVisitor::visitReadingClause(const BoundReadingClause& readingClause) {
switch (readingClause.getClauseType()) {
case common::ClauseType::MATCH: {
visitMatch(readingClause);
} break;
case common::ClauseType::UNWIND: {
visitUnwind(readingClause);
} break;
default:
throw NotImplementedException("BoundStatementVisitor::visitReadingClause");
}
}

void BoundStatementVisitor::visitUpdatingClause(const BoundUpdatingClause& updatingClause) {
switch (updatingClause.getClauseType()) {
case common::ClauseType::SET: {
visitSet(updatingClause);
} break;
case common::ClauseType::DELETE: {
visitDelete(updatingClause);
} break;
case common::ClauseType::CREATE: {
visitCreate(updatingClause);
} break;
default:
throw NotImplementedException("BoundStatementVisitor::visitUpdatingClause");
}
}

} // namespace binder
} // namespace kuzu
2 changes: 0 additions & 2 deletions src/binder/query/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ add_library(
bound_delete_clause.cpp
bound_projection_body.cpp
bound_set_clause.cpp
normalized_query_part.cpp
normalized_single_query.cpp
query_graph.cpp)

set(ALL_OBJECT_FILES
Expand Down
10 changes: 0 additions & 10 deletions src/binder/query/bound_create_clause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@ std::vector<expression_pair> BoundCreateClause::getAllSetItems() const {
return result;
}

expression_vector BoundCreateClause::getPropertiesToRead() const {
expression_vector result;
for (auto& setItem : getAllSetItems()) {
for (auto& property : setItem.second->getSubPropertyExpressions()) {
result.push_back(property);
}
}
return result;
}

std::unique_ptr<BoundUpdatingClause> BoundCreateClause::copy() {
auto result = std::make_unique<BoundCreateClause>();
for (auto& createNode : createNodes) {
Expand Down
13 changes: 0 additions & 13 deletions src/binder/query/bound_delete_clause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,6 @@
namespace kuzu {
namespace binder {

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

std::unique_ptr<BoundUpdatingClause> BoundDeleteClause::copy() {
auto result = std::make_unique<BoundDeleteClause>();
for (auto& deleteNode : deleteNodes) {
Expand Down
15 changes: 0 additions & 15 deletions src/binder/query/bound_projection_body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,5 @@ void BoundProjectionBody::setOrderByExpressions(
isAscOrders = std::move(sortOrders);
}

expression_vector BoundProjectionBody::getPropertiesToRead() const {
expression_vector result;
for (auto& expression : projectionExpressions) {
for (auto& property : expression->getSubPropertyExpressions()) {
result.push_back(property);
}
}
for (auto& expression : orderByExpressions) {
for (auto& property : expression->getSubPropertyExpressions()) {
result.push_back(property);
}
}
return result;
}

} // namespace binder
} // namespace kuzu
15 changes: 0 additions & 15 deletions src/binder/query/bound_set_clause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,6 @@
namespace kuzu {
namespace binder {

expression_vector BoundSetClause::getPropertiesToRead() const {
expression_vector result;
for (auto& setNodeProperty : setNodeProperties) {
for (auto& property : setNodeProperty->getSetItem().second->getSubPropertyExpressions()) {
result.push_back(property);
}
}
for (auto& setRelProperty : setRelProperties) {
for (auto& property : setRelProperty->getSetItem().second->getSubPropertyExpressions()) {
result.push_back(property);
}
}
return result;
}

std::unique_ptr<BoundUpdatingClause> BoundSetClause::copy() {
auto result = std::make_unique<BoundSetClause>();
for (auto& setNodeProperty : setNodeProperties) {
Expand Down
33 changes: 0 additions & 33 deletions src/binder/query/normalized_query_part.cpp

This file was deleted.

17 changes: 0 additions & 17 deletions src/binder/query/normalized_single_query.cpp

This file was deleted.

8 changes: 8 additions & 0 deletions src/binder/visitor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
add_library(
kuzu_binder_visitor
OBJECT
property_collector.cpp)

set(ALL_OBJECT_FILES
${ALL_OBJECT_FILES} $<TARGET_OBJECTS:kuzu_binder_visitor>
PARENT_SCOPE)
93 changes: 93 additions & 0 deletions src/binder/visitor/property_collector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include "binder/visitor/property_collector.h"

#include "binder/query/reading_clause/bound_match_clause.h"
#include "binder/query/reading_clause/bound_unwind_clause.h"
#include "binder/query/updating_clause/bound_create_clause.h"
#include "binder/query/updating_clause/bound_delete_clause.h"
#include "binder/query/updating_clause/bound_set_clause.h"

namespace kuzu {
namespace binder {

expression_vector PropertyCollector::getProperties() {
expression_vector result;
for (auto& property : properties) {
result.push_back(property);
}
return result;
}

void PropertyCollector::visitMatch(const BoundReadingClause& readingClause) {
auto& matchClause = (BoundMatchClause&)readingClause;
for (auto& rel : matchClause.getQueryGraphCollection()->getQueryRels()) {
if (rel->hasInternalIDProperty()) {
properties.insert(rel->getInternalIDProperty());
}
}
if (matchClause.hasWhereExpression()) {
collectPropertyExpressions(*matchClause.getWhereExpression());
}
}

void PropertyCollector::visitUnwind(const BoundReadingClause& readingClause) {
auto& unwindClause = (BoundUnwindClause&)readingClause;
collectPropertyExpressions(*unwindClause.getExpression());
}

void PropertyCollector::visitSet(const BoundUpdatingClause& updatingClause) {
auto& boundSetClause = (BoundSetClause&)updatingClause;
for (auto& setNodeProperty : boundSetClause.getSetNodeProperties()) {
collectPropertyExpressions(*setNodeProperty->getSetItem().second);
}
for (auto& setRelProperty : boundSetClause.getSetRelProperties()) {
collectPropertyExpressions(*setRelProperty->getSetItem().second);
}
}

void PropertyCollector::visitDelete(const BoundUpdatingClause& updatingClause) {
auto& boundDeleteClause = (BoundDeleteClause&)updatingClause;
for (auto& deleteNode : boundDeleteClause.getDeleteNodes()) {
properties.insert(deleteNode->getPrimaryKeyExpression());
}
for (auto& deleteRel : boundDeleteClause.getDeleteRels()) {
if (deleteRel->hasInternalIDProperty()) {
properties.insert(deleteRel->getInternalIDProperty());
}
}
}

void PropertyCollector::visitCreate(const BoundUpdatingClause& updatingClause) {
auto& boundCreateClause = (BoundCreateClause&)updatingClause;
for (auto& createNode : boundCreateClause.getCreateNodes()) {
for (auto& setItem : createNode->getSetItems()) {
collectPropertyExpressions(*setItem.second);
}
}
for (auto& createRel : boundCreateClause.getCreateRels()) {
for (auto& setItem : createRel->getSetItems()) {
collectPropertyExpressions(*setItem.second);
}
}
}

void PropertyCollector::visitProjectionBody(const BoundProjectionBody& projectionBody) {
for (auto& expression : projectionBody.getProjectionExpressions()) {
collectPropertyExpressions(*expression);
}
for (auto& expression : projectionBody.getOrderByExpressions()) {
collectPropertyExpressions(*expression);
}
}

void PropertyCollector::visitProjectionBodyPredicate(Expression& predicate) {
collectPropertyExpressions(predicate);
}

void PropertyCollector::collectPropertyExpressions(Expression& expression) {
for (auto& property : expression.getSubPropertyExpressions()) {
properties.insert(property);
}
}

} // namespace binder
} // namespace kuzu
3 changes: 0 additions & 3 deletions src/include/binder/bound_statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ class BoundStatement {

inline BoundStatementResult* getStatementResult() const { return statementResult.get(); }

inline bool isDDL() const { return common::StatementTypeUtils::isDDL(statementType); }
inline bool isCopyCSV() const { return common::StatementTypeUtils::isCopyCSV(statementType); }

virtual inline bool isReadOnly() const { return false; }

private:
Expand Down
Loading

0 comments on commit 80e8fb0

Please sign in to comment.