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

add op_node_index_seek_byrange #650

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
fix code format
  • Loading branch information
ncbd committed Aug 29, 2024
commit f29ff79ae30f3d974955c5a0b1b2d301ad13ca6b
3 changes: 2 additions & 1 deletion src/cypher/execution_plan/ops/op.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ struct OpBase {

bool IsScan() const {
return type == OpType::ALL_NODE_SCAN || type == OpType::NODE_BY_LABEL_SCAN ||
type == OpType::NODE_INDEX_SEEK || type == OpType::ARGUMENT || type == OpType::NODE_INDEX_SEEK_BYRANGE;
type == OpType::NODE_INDEX_SEEK || type == OpType::ARGUMENT ||
type == OpType::NODE_INDEX_SEEK_BYRANGE;
}

bool IsDynamicScan() const {
Expand Down
16 changes: 15 additions & 1 deletion src/cypher/execution_plan/ops/op_node_index_seek_byrange.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
#include "cypher/execution_plan/ops/op_node_index_seek_byrange.h"
/**
* Copyright 2022 AntGroup CO., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/

#include "cypher/execution_plan/ops/op_node_index_seek_byrange.h"
60 changes: 19 additions & 41 deletions src/cypher/execution_plan/ops/op_node_index_seek_byrange.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include "core/data_type.h"
#include "cypher/execution_plan/ops/op.h"
#include "cypher_exception.h"
#include "cypher/cypher_exception.h"

namespace cypher {

Expand All @@ -34,21 +34,12 @@ class NodeIndexSeekByRange : public OpBase {
std::vector<lgraph::FieldData> target_values_;
lgraph::CompareOp cmpOp_;

OpResult HandOff() {
if (!it_ || !it_->IsValid()) return OP_REFRESH;
if (!consuming_) {
consuming_ = true;
return OP_OK;
}
it_->Next();
return it_->IsValid() ? OP_OK : OP_REFRESH;
}

public:
NodeIndexSeekByRange(Node *node, const SymbolTable *sym_tab, std::string field = "",
std::vector<lgraph::FieldData> target_values = {},
lgraph::CompareOp cmpOP = lgraph::CompareOp::LBR_EQ)
: OpBase(OpType::NODE_INDEX_SEEK_BYRANGE, "Node Index Seek By Range"), node_(node), sym_tab_(sym_tab), cmpOp_(cmpOP) {
: OpBase(OpType::NODE_INDEX_SEEK_BYRANGE, "Node Index Seek By Range"),
node_(node), sym_tab_(sym_tab), cmpOp_(cmpOP) {
if (node) {
it_ = node->ItRef();
alias_ = node->Alias();
Expand Down Expand Up @@ -85,25 +76,30 @@ class NodeIndexSeekByRange : public OpBase {
CYPHER_TODO();
}
CYPHER_THROW_ASSERT(!target_values_.empty());
CYPHER_THROW_ASSERT(cmpOp_ == lgraph::CompareOp::LBR_GT || cmpOp_ == lgraph::CompareOp::LBR_LT);
CYPHER_THROW_ASSERT(cmpOp_ == lgraph::CompareOp::LBR_GT ||
cmpOp_ == lgraph::CompareOp::LBR_LT);
auto value = target_values_[0];
if (cmpOp_ == lgraph::CompareOp::LBR_GT) {
if (!node_->Label().empty() && ctx->txn_->GetTxn()->IsIndexed(node_->Label(), field_)) {
it_->Initialize(ctx->txn_->GetTxn().get(), lgraph::VIter::INDEX_ITER, node_->Label(),
field_, value, lgraph::FieldData());
if (!node_->Label().empty() && ctx->txn_->GetTxn()
->IsIndexed(node_->Label(), field_)) {
it_->Initialize(ctx->txn_->GetTxn().get(),
lgraph::VIter::INDEX_ITER, node_->Label(),
field_, value, lgraph::FieldData());
} else {
// Weak index iterator
it_->Initialize(ctx->txn_->GetTxn().get(), node_->Label(), field_, value, lgraph::FieldData());
//CYPHER_THROW_ASSERT(0);
it_->Initialize(ctx->txn_->GetTxn().get(), node_->Label(),
field_, value, lgraph::FieldData());
}
} else if (cmpOp_ == lgraph::CompareOp::LBR_LT) {
if (!node_->Label().empty() && ctx->txn_->GetTxn()->IsIndexed(node_->Label(), field_)) {
it_->Initialize(ctx->txn_->GetTxn().get(), lgraph::VIter::INDEX_ITER, node_->Label(),
field_, lgraph::FieldData(), value);
if (!node_->Label().empty() &&
ctx->txn_->GetTxn()->IsIndexed(node_->Label(), field_)) {
it_->Initialize(ctx->txn_->GetTxn().get(),
lgraph::VIter::INDEX_ITER, node_->Label(),
field_, lgraph::FieldData(), value);
} else {
// Weak index iterator
it_->Initialize(ctx->txn_->GetTxn().get(), node_->Label(), field_, lgraph::FieldData(), value);
//CYPHER_THROW_ASSERT(0);
it_->Initialize(ctx->txn_->GetTxn().get(), node_->Label(),
field_, lgraph::FieldData(), value);
}
}
consuming_ = false;
Expand All @@ -129,24 +125,6 @@ class NodeIndexSeekByRange : public OpBase {
LOG_DEBUG() << alias_ << ": " << it_->GetId();
#endif
return OP_OK;
// if (HandOff() == OP_OK) {
// return OP_OK;
// }
// while ((size_t)value_rec_idx_ < target_values_.size() - 1) {
// value_rec_idx_++;
// auto value = target_values_[value_rec_idx_];
// if (!node_->Label().empty() && ctx->txn_->GetTxn()->IsIndexed(node_->Label(), field_)) {
// it_->Initialize(ctx->txn_->GetTxn().get(), lgraph::VIter::INDEX_ITER,
// node_->Label(), field_, value, value);
// } else {
// // Weak index iterator
// it_->Initialize(ctx->txn_->GetTxn().get(), node_->Label(), field_, value);
// }
// if (it_->IsValid()) {
// return OP_OK;
// }
// }
// return OP_DEPLETED;
}

OpResult ResetImpl(bool complete = false) override {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,18 @@ class LocateNodeByIndexedProp : public OptPass {
// Reason that not to build unwind:unwind needs one more symbol in symbol table
if (target_value_datas.empty()) target_value_datas.emplace_back(value);
if (cmpOp == lgraph::CompareOp::LBR_EQ) {
auto op_node_index_seek = new NodeIndexSeek(node, symtab, field, target_value_datas);
auto op_node_index_seek = new NodeIndexSeek(node, symtab,
field, target_value_datas);
op_node_index_seek->parent = op_post;
op_post->RemoveChild(op_filter);
OpBase::FreeStream(op_filter);
op_filter = nullptr;
op_post->AddChild(op_node_index_seek);
} else if (cmpOp == lgraph::CompareOp::LBR_GT || cmpOp == lgraph::CompareOp::LBR_LT) {
auto op_node_index_seek_byrange = new NodeIndexSeekByRange(node, symtab, field, target_value_datas, cmpOp);
} else if (cmpOp == lgraph::CompareOp::LBR_GT ||
cmpOp == lgraph::CompareOp::LBR_LT) {
auto op_node_index_seek_byrange
= new NodeIndexSeekByRange(node, symtab,
field, target_value_datas, cmpOp);
op_node_index_seek_byrange->parent = op_post;
op_post->RemoveChild(op_filter);
OpBase::FreeStream(op_filter);
Expand Down Expand Up @@ -182,7 +186,7 @@ class LocateNodeByIndexedProp : public OptPass {
} else if ((filter->Type() == lgraph::Filter::BINARY &&
filter->LogicalOp() == lgraph::LBR_OR)) {
if (!getValueFromRangeFilter(filter->Right(), field, value, cmpOP)) return false;
if(cmpOP != lgraph::CompareOp::LBR_EQ) return false;
if (cmpOP != lgraph::CompareOp::LBR_EQ) return false;
target_value_datas.emplace_back(value);
while (!filter->Left()->IsLeaf()) {
filter = filter->Left();
Expand All @@ -191,11 +195,11 @@ class LocateNodeByIndexedProp : public OptPass {
return false;
}
if (!getValueFromRangeFilter(filter->Right(), field, value, cmpOP)) return false;
if(cmpOP != lgraph::CompareOp::LBR_EQ) return false;
if (cmpOP != lgraph::CompareOp::LBR_EQ) return false;
target_value_datas.emplace_back(value);
}
if (!getValueFromRangeFilter(filter->Left(), field, value, cmpOP)) return false;
if(cmpOP != lgraph::CompareOp::LBR_EQ) return false;
if (cmpOP != lgraph::CompareOp::LBR_EQ) return false;
target_value_datas.emplace_back(value);
std::reverse(target_value_datas.begin(), target_value_datas.end());
return true;
Expand All @@ -220,7 +224,8 @@ class LocateNodeByIndexedProp : public OptPass {
}

for (auto child : op->children) {
if (FindNodePropFilter(child, op_filter, field, value, target_value_datas, cmpOp)) return true;
if (FindNodePropFilter(child, op_filter, field,
value, target_value_datas, cmpOp)) return true;
}

return false;
Expand Down
35 changes: 21 additions & 14 deletions src/cypher/filter/iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class WeakIndexIterator {

void SeekIndex(const std::string &l) {
if (_txn->IsIndexed(l, _field_name)) {
if(!range) {
if (!range) {
auto iit = _txn->GetVertexIndexIterator(l, _field_name, _value, _value);
if (iit.IsValid()) {
_iit.emplace_back(new lgraph::VertexIndexIterator(std::move(iit)));
Expand Down Expand Up @@ -150,9 +150,11 @@ class WeakIndexIterator {
}
}

WeakIndexIterator(lgraph::Transaction *txn, const std::string &label, const std::string &field,
const FieldData &startVal, const FieldData &endVal)
: _txn(txn), _label(label), _field_name(field), _startVal(startVal), _endVal(endVal), range(true) {
WeakIndexIterator(lgraph::Transaction *txn, const std::string &label,
const std::string &field, const FieldData &startVal,
const FieldData &endVal)
: _txn(txn), _label(label), _field_name(field),
_startVal(startVal), _endVal(endVal), range(true) {
if (!_label.empty()) {
SeekIndex(_label);
} else {
Expand All @@ -166,7 +168,7 @@ class WeakIndexIterator {
// TODO(anyone) optimize, use label iterator
_it = new lgraph::graph::VertexIterator(_txn->GetVertexIterator());
while (_it->IsValid()) {
if(endVal.type == lgraph_api::NUL) {
if (endVal.type == lgraph_api::NUL) {
if ((_label.empty() || _txn->GetVertexLabel(*_it) == _label) &&
_txn->GetVertexField(*_it, _field_name) > startVal) {
_valid = true;
Expand Down Expand Up @@ -221,14 +223,15 @@ class WeakIndexIterator {
if (_it) {
while (_it->IsValid()) {
_it->Next();
if(!range){
if (_it->IsValid() && (_label.empty() || _txn->GetVertexLabel(*_it) == _label) &&
if (!range) {
if (_it->IsValid() && (_label.empty() ||
_txn->GetVertexLabel(*_it) == _label) &&
_txn->GetVertexField(*_it, _field_name) == _value) {
_valid = true;
break;
}
} else {
if(_endVal.type == lgraph_api::NUL) {
if (_endVal.type == lgraph_api::NUL) {
if ((_label.empty() || _txn->GetVertexLabel(*_it) == _label) &&
_txn->GetVertexField(*_it, _field_name) > _startVal) {
_valid = true;
Expand All @@ -254,27 +257,31 @@ class WeakIndexIterator {
auto iit = _iit[_iit_idx];
while (iit->IsValid()) {
iit->Next();
if(!range) {
if (iit->IsValid() && (_label.empty() || _txn->GetVertexLabel(*_it) == _label) &&
if (!range) {
if (iit->IsValid() && (_label.empty() ||
_txn->GetVertexLabel(*_it) == _label) &&
_txn->GetVertexField(iit->GetVid(), _field_name) == _value) {
_valid = true;
break;
}
} else {
if(_endVal.type == lgraph_api::NUL) {
if (iit->IsValid() && (_label.empty() || _txn->GetVertexLabel(*_it) == _label) &&
if (_endVal.type == lgraph_api::NUL) {
if (iit->IsValid() && (_label.empty() ||
_txn->GetVertexLabel(*_it) == _label) &&
_txn->GetVertexField(iit->GetVid(), _field_name) > _startVal) {
_valid = true;
break;
}
} else if (_startVal.type == lgraph_api::NUL) {
if (iit->IsValid() && (_label.empty() || _txn->GetVertexLabel(*_it) == _label) &&
if (iit->IsValid() && (_label.empty() ||
_txn->GetVertexLabel(*_it) == _label) &&
_txn->GetVertexField(iit->GetVid(), _field_name) < _endVal) {
_valid = true;
break;
}
} else {
if (iit->IsValid() && (_label.empty() || _txn->GetVertexLabel(*_it) == _label) &&
if (iit->IsValid() && (_label.empty() ||
_txn->GetVertexLabel(*_it) == _label) &&
_txn->GetVertexField(iit->GetVid(), _field_name) > _startVal &&
_txn->GetVertexField(iit->GetVid(), _field_name) < _endVal) {
_valid = true;
Expand Down
Loading