Skip to content

Commit

Permalink
implement parameters query
Browse files Browse the repository at this point in the history
  • Loading branch information
jiazhenjiang authored and tugraph committed Feb 2, 2024
1 parent a82bf84 commit 7581761
Show file tree
Hide file tree
Showing 15 changed files with 26 additions and 21 deletions.
3 changes: 1 addition & 2 deletions src/cypher/execution_plan/ops/op_all_node_scan.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ class AllNodeScan : public OpBase {

OpResult Initialize(RTContext *ctx) override {
// allocate a new record
record = std::make_shared<Record>(rec_length_, sym_tab_);
record = std::make_shared<Record>(rec_length_, sym_tab_, ctx->param_tab_);
record->values[node_rec_idx_].type = Entry::NODE;
record->values[node_rec_idx_].node = node_;
record->SetParameter(ctx->param_tab_);
// transaction allocated before in plan:execute
// TODO(anyone) remove patternGraph's state (ctx)
node_->ItRef()->Initialize(ctx->txn_->GetTxn().get(), lgraph::VIter::VERTEX_ITER);
Expand Down
3 changes: 1 addition & 2 deletions src/cypher/execution_plan/ops/op_argument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ void Argument::Receive(std::shared_ptr<Record> *input_record, PatternGraph *patt

OpBase::OpResult Argument::Initialize(RTContext *ctx) {
// allocate a new record
record = std::make_shared<Record>(sym_tab_->symbols.size(), sym_tab_);
record->SetParameter(ctx->param_tab_);
record = std::make_shared<Record>(sym_tab_->symbols.size(), sym_tab_, ctx->param_tab_);
for (auto &arg : args_) {
if (arg.type == SymbolNode::NODE) {
auto &node = pattern_graph_->GetNode(arg.alias);
Expand Down
2 changes: 1 addition & 1 deletion src/cypher/execution_plan/ops/op_cartesian_product.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class CartesianProduct : public OpBase {
}
}
if (!sym_tab) throw lgraph::CypherException("CartesianProduct initialize failed");
record = std::make_shared<Record>(sym_tab->symbols.size(), sym_tab);
record = std::make_shared<Record>(sym_tab->symbols.size(), sym_tab, ctx->param_tab_);
return OP_OK;
}

Expand Down
3 changes: 2 additions & 1 deletion src/cypher/execution_plan/ops/op_create.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ class OpCreate : public OpBase {
for (auto child : children) {
child->Initialize(ctx);
}
record = children.empty() ? std::make_shared<Record>(sym_tab_.symbols.size(), &sym_tab_)
record = children.empty() ?
std::make_shared<Record>(sym_tab_.symbols.size(), &sym_tab_, ctx->param_tab_)
: children[0]->record;
return OP_OK;
}
Expand Down
7 changes: 4 additions & 3 deletions src/cypher/execution_plan/ops/op_immediate_argument.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ImmediateArgument : public OpBase {
std::vector<ArgIndex> args_;

public:
ImmediateArgument(RTContext *ctx, PatternGraph *pattern)
explicit ImmediateArgument(PatternGraph *pattern)
: OpBase(OpType::ARGUMENT, "Immediate Argument"), pattern_graph_(pattern) {
auto sym_tab = &pattern_graph_->symbol_table;
for (auto &s : sym_tab->symbols) {
Expand All @@ -37,13 +37,14 @@ class ImmediateArgument : public OpBase {
args_.emplace_back(s.first, s.second.id, s.second.type);
}
}
// allocate a new record
record = std::make_shared<Record>(sym_tab->symbols.size(), sym_tab);
}

void Receive(const Record *input_record) { input_record_ = input_record; }

OpResult Initialize(RTContext *ctx) override {
auto sym_tab = &pattern_graph_->symbol_table;
// allocate a new record
record = std::make_shared<Record>(sym_tab->symbols.size(), sym_tab, ctx->param_tab_);
for (auto &arg : args_) {
if (arg.type == SymbolNode::NODE) {
auto &node = pattern_graph_->GetNode(arg.alias);
Expand Down
2 changes: 1 addition & 1 deletion src/cypher/execution_plan/ops/op_inquery_call.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class InQueryCall : public OpBase {
OpResult Initialize(RTContext *ctx) override {
auto &sym_tab = pattern_->symbol_table;
if (children.empty()) {
record = std::make_shared<Record>(sym_tab.symbols.size(), &sym_tab);
record = std::make_shared<Record>(sym_tab.symbols.size(), &sym_tab, ctx->param_tab_);
} else {
CYPHER_THROW_ASSERT(children.size() == 1);
if (children[0]->Initialize(ctx) != OP_OK) return OP_ERR;
Expand Down
3 changes: 2 additions & 1 deletion src/cypher/execution_plan/ops/op_merge.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,8 @@ class OpMerge : public OpBase {
for (auto child : children) {
child->Initialize(ctx);
}
record = children.empty() ? std::make_shared<Record>(sym_tab_.symbols.size(), &sym_tab_)
record = children.empty() ?
std::make_shared<Record>(sym_tab_.symbols.size(), &sym_tab_, ctx->param_tab_)
: children[0]->record;
return OP_OK;
}
Expand Down
2 changes: 1 addition & 1 deletion src/cypher/execution_plan/ops/op_node_by_id_seek.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class NodeByIdSeek : public OpBase {
}

OpResult Initialize(RTContext *ctx) override {
record = std::make_shared<Record>(rec_length_, sym_tab_);
record = std::make_shared<Record>(rec_length_, sym_tab_, ctx->param_tab_);
record->values[node_rec_idx_].type = Entry::NODE;
record->values[node_rec_idx_].node = node_;
record->SetParameter(ctx->param_tab_);
Expand Down
3 changes: 1 addition & 2 deletions src/cypher/execution_plan/ops/op_node_by_label_scan.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ class NodeByLabelScan : public OpBase {

OpResult Initialize(RTContext *ctx) override {
// allocate a new record
record = std::make_shared<Record>(rec_length_, sym_tab_);
record = std::make_shared<Record>(rec_length_, sym_tab_, ctx->param_tab_);
record->values[node_rec_idx_].type = Entry::NODE;
record->values[node_rec_idx_].node = node_;
record->SetParameter(ctx->param_tab_);
// transaction allocated before in plan:execute
// TODO(anyone) remove patternGraph's state (ctx)
auto primary_filed = ctx->txn_->GetVertexPrimaryField(node_->Label());
Expand Down
3 changes: 1 addition & 2 deletions src/cypher/execution_plan/ops/op_node_index_seek.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,9 @@ class NodeIndexSeek : public OpBase {

OpResult Initialize(RTContext *ctx) override {
// allocate a new record
record = std::make_shared<Record>(rec_length_, sym_tab_);
record = std::make_shared<Record>(rec_length_, sym_tab_, ctx->param_tab_);
record->values[node_rec_idx_].type = Entry::NODE;
record->values[node_rec_idx_].node = node_;
record->SetParameter(ctx->param_tab_);

auto &pf = node_->Prop();
field_ = pf.type != Property::NUL ? pf.field : field_; // use pf.field if applicable
Expand Down
2 changes: 1 addition & 1 deletion src/cypher/execution_plan/ops/op_unwind.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Unwind : public OpBase {
if (children.size() > 1) CYPHER_TODO();
if (children.empty()) {
// No child operation, list must be static.
record = std::make_shared<Record>(sym_tab_->symbols.size(), sym_tab_);
record = std::make_shared<Record>(sym_tab_->symbols.size(), sym_tab_, ctx->param_tab_);
// Set parameter, use uniform runtime context
for (auto &p : ctx->param_tab_) {
auto it = record->symbol_table->symbols.find(p.first);
Expand Down
3 changes: 1 addition & 2 deletions src/cypher/filter/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ void TestExists::BuildNestedExecutionPlan() {
if (expand_streams.size() > 1) CYPHER_TODO();
CYPHER_THROW_ASSERT(!expand_streams.empty() && !expand_streams[0].empty());
std::vector<cypher::OpBase *> expand_ops;
// TODO(anyone) use dummy RTContext here.
auto *ia = new cypher::ImmediateArgument(nullptr, nested_pattern_.get());
auto *ia = new cypher::ImmediateArgument(nested_pattern_.get());
expand_ops.emplace_back(ia);
for (auto &step : expand_streams[0]) {
auto &start = nested_pattern_->GetNode(std::get<0>(step));
Expand Down
2 changes: 1 addition & 1 deletion src/cypher/resultset/record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ lgraph::FieldData Entry::GetEntityField(RTContext *ctx, const std::string &fd) c
}

void Record::SetParameter(const PARAM_TAB &ptab) {
if (!symbol_table) return;
if (!symbol_table || ptab.empty()) return;
for (auto &param : ptab) {
auto it = symbol_table->symbols.find(param.first);
if (it != symbol_table->symbols.end()) {
Expand Down
6 changes: 5 additions & 1 deletion src/cypher/resultset/record.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,11 @@ struct Record {

explicit Record(size_t size) { values.resize(size); }

Record(size_t size, const SymbolTable *sym_tab) : symbol_table(sym_tab) { values.resize(size); }
Record(size_t size, const SymbolTable *sym_tab, const PARAM_TAB &ptab)
: symbol_table(sym_tab) {
values.resize(size);
SetParameter(ptab);
}

Record(const Record &rhs) {
values = rhs.values;
Expand Down
3 changes: 3 additions & 0 deletions test/test_cypher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ using namespace antlr4;

static const cypher::PARAM_TAB g_param_tab = {
{"$name", cypher::FieldData(lgraph::FieldData("Lindsay Lohan"))},
{"$new_name", cypher::FieldData(lgraph::FieldData("new name"))},
{"$personId", cypher::FieldData(lgraph::FieldData(1))},
{"$personIds", cypher::FieldData(std::vector<lgraph::FieldData>{
lgraph::FieldData("Liam Neeson"), lgraph::FieldData("Dennis Quaid"),
Expand Down Expand Up @@ -501,6 +502,8 @@ int test_parameter(cypher::RTContext *ctx) {
static const std::vector<std::pair<std::string, int>> script_check = {
{"MATCH (n:Person) WHERE n.name = $name RETURN n", 1},
{"MATCH (n:Person {name:$name}) RETURN n", 1},
{"CREATE (n:Person {name:$new_name}) RETURN n", 1},
{"MATCH (n:Person {name:$new_name}) DELETE n", 1},
};
std::vector<std::string> scripts;
std::vector<int> check;
Expand Down

0 comments on commit 7581761

Please sign in to comment.