Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check statement read only through visitor pattern #1559

Merged
merged 1 commit into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/binder/visitor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
add_library(
kuzu_binder_visitor
OBJECT
property_collector.cpp)
property_collector.cpp
statement_read_write_analyzer.cpp)

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

namespace kuzu {
namespace binder {

bool StatementReadWriteAnalyzer::isReadOnly(const kuzu::binder::BoundStatement& statement) {
visit(statement);
return readOnly;
}

void StatementReadWriteAnalyzer::visitQueryPart(const NormalizedQueryPart& queryPart) {
readOnly = !queryPart.hasUpdatingClause();
}

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

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

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

private:
common::StatementType statementType;
std::unique_ptr<BoundStatementResult> statementResult;
Expand Down
6 changes: 3 additions & 3 deletions src/include/binder/bound_statement_visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class BoundStatementVisitor {

void visit(const BoundStatement& statement);

void visitRegularQuery(const BoundRegularQuery& regularQuery);
void visitSingleQuery(const NormalizedSingleQuery& singleQuery);
void visitQueryPart(const NormalizedQueryPart& queryPart);
virtual void visitRegularQuery(const BoundRegularQuery& regularQuery);
virtual void visitSingleQuery(const NormalizedSingleQuery& singleQuery);
virtual void visitQueryPart(const NormalizedQueryPart& queryPart);

protected:
virtual void visitCreateNodeTable(const BoundStatement& statement) {}
Expand Down
9 changes: 0 additions & 9 deletions src/include/binder/query/bound_regular_query.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@ class BoundRegularQuery : public BoundStatement {

~BoundRegularQuery() override = default;

inline bool isReadOnly() const override {
for (auto& singleQuery : singleQueries) {
if (!singleQuery->isReadOnly()) {
return false;
}
}
return true;
}

inline void addSingleQuery(std::unique_ptr<NormalizedSingleQuery> singleQuery) {
singleQueries.push_back(std::move(singleQuery));
}
Expand Down
9 changes: 0 additions & 9 deletions src/include/binder/query/normalized_single_query.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@ class NormalizedSingleQuery {
NormalizedSingleQuery() = default;
~NormalizedSingleQuery() = default;

inline bool isReadOnly() const {
for (auto& queryPart : queryParts) {
if (queryPart->hasUpdatingClause()) {
return false;
}
}
return true;
}

inline void appendQueryPart(std::unique_ptr<NormalizedQueryPart> queryPart) {
queryParts.push_back(std::move(queryPart));
}
Expand Down
22 changes: 22 additions & 0 deletions src/include/binder/visitor/statement_read_write_analyzer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "binder/bound_statement_visitor.h"

namespace kuzu {
namespace binder {

class StatementReadWriteAnalyzer : public BoundStatementVisitor {
public:
StatementReadWriteAnalyzer() : BoundStatementVisitor(), readOnly{false} {}

bool isReadOnly(const BoundStatement& statement);

private:
void visitQueryPart(const NormalizedQueryPart& queryPart) final;

private:
bool readOnly;
};

} // namespace binder
} // namespace kuzu
4 changes: 3 additions & 1 deletion src/main/connection.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "main/connection.h"

#include "binder/binder.h"
#include "binder/visitor/statement_read_write_analyzer.h"
#include "json.hpp"
#include "main/database.h"
#include "main/plan_printer.h"
Expand Down Expand Up @@ -165,7 +166,8 @@ std::unique_ptr<PreparedStatement> Connection::prepareNoLock(
auto binder = Binder(*database->catalog);
auto boundStatement = binder.bind(*statement);
preparedStatement->statementType = boundStatement->getStatementType();
preparedStatement->readOnly = boundStatement->isReadOnly();
preparedStatement->readOnly =
binder::StatementReadWriteAnalyzer().isReadOnly(*boundStatement);
preparedStatement->parameterMap = binder.getParameterMap();
preparedStatement->statementResult = boundStatement->getStatementResult()->copy();
// planning
Expand Down