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

Merge limit and full text index scan rule #5577

Merged
merged 3 commits into from
Jun 7, 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
1 change: 1 addition & 0 deletions src/graph/optimizer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ nebula_add_library(
rule/MergeGetVerticesAndProjectRule.cpp
rule/MergeGetNbrsAndDedupRule.cpp
rule/MergeGetNbrsAndProjectRule.cpp
rule/MergeLimitAndFulltextIndexScanRule.cpp
rule/IndexScanRule.cpp
rule/PushLimitDownGetNeighborsRule.cpp
rule/PushLimitDownGetVerticesRule.cpp
Expand Down
92 changes: 92 additions & 0 deletions src/graph/optimizer/rule/MergeLimitAndFulltextIndexScanRule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* Copyright (c) 2020 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/

#include "graph/optimizer/rule/MergeLimitAndFulltextIndexScanRule.h"

#include "graph/optimizer/OptContext.h"
#include "graph/optimizer/OptGroup.h"
#include "graph/planner/plan/PlanNode.h"
#include "graph/planner/plan/Query.h"
#include "graph/util/ExpressionUtils.h"

using nebula::graph::Explore;
using nebula::graph::FulltextIndexScan;
using nebula::graph::Limit;
using nebula::graph::PlanNode;
using nebula::graph::QueryContext;

namespace nebula {
namespace opt {

std::unique_ptr<OptRule> MergeLimitAndFulltextIndexScanRule::kInstance =
std::unique_ptr<MergeLimitAndFulltextIndexScanRule>(new MergeLimitAndFulltextIndexScanRule());

MergeLimitAndFulltextIndexScanRule::MergeLimitAndFulltextIndexScanRule() {
RuleSet::QueryRules().addRule(this);
}

const Pattern &MergeLimitAndFulltextIndexScanRule::pattern() const {
static Pattern pattern = Pattern::create(
PlanNode::Kind::kLimit,
{Pattern::create({graph::PlanNode::Kind::kGetVertices, graph::PlanNode::Kind::kGetEdges},
{Pattern::create(graph::PlanNode::Kind::kFulltextIndexScan)})});
return pattern;
}

bool MergeLimitAndFulltextIndexScanRule::match(OptContext *, const MatchedResult &matched) const {
auto limit = static_cast<const Limit *>(matched.planNode());
if (limit->offset() <= 0) {
jievince marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
auto explore = static_cast<const Explore *>(matched.planNode({0, 0}));
if (explore->limit() <= 0) {
return false;
}
auto ft = static_cast<const FulltextIndexScan *>(matched.planNode({0, 0, 0}));
return ft->limit() >= 0 && ft->offset() < 0;
}

StatusOr<OptRule::TransformResult> MergeLimitAndFulltextIndexScanRule::transform(
OptContext *octx, const MatchedResult &matched) const {
auto limitGroupNode = matched.result().node;
auto exploreGroupNode = matched.result({0, 0}).node;
auto ftGroupNode = matched.result({0, 0, 0}).node;

const auto limit = static_cast<const Limit *>(limitGroupNode->node());
const auto explore = static_cast<const Explore *>(exploreGroupNode->node());
const auto ft = static_cast<const FulltextIndexScan *>(ftGroupNode->node());

auto limitRows = limit->count() + limit->offset();
if (limitRows != explore->limit() || limitRows != ft->limit()) {
return TransformResult::noTransform();
}

auto newExplore = static_cast<Explore *>(explore->clone());
newExplore->setOutputVar(limit->outputVar());
auto newExploreGroupNode = OptGroupNode::create(octx, newExplore, limitGroupNode->group());

auto newFt = static_cast<FulltextIndexScan *>(ft->clone());
newFt->setOffset(limit->offset());
auto newFtGroup = OptGroup::create(octx);
auto newFtGroupNode = newFtGroup->makeGroupNode(newFt);

newExploreGroupNode->dependsOn(newFtGroup);
newExplore->setInputVar(newFt->outputVar());
for (auto dep : ftGroupNode->dependencies()) {
newFtGroupNode->dependsOn(dep);
}

TransformResult result;
result.eraseAll = true;
result.newGroupNodes.emplace_back(newExploreGroupNode);
return result;
}

std::string MergeLimitAndFulltextIndexScanRule::toString() const {
return "MergeLimitAndFulltextIndexScanRule";
}

} // namespace opt
} // namespace nebula
68 changes: 68 additions & 0 deletions src/graph/optimizer/rule/MergeLimitAndFulltextIndexScanRule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* Copyright (c) 2020 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/

#ifndef GRAPH_OPTIMIZER_RULE_MERGELIMITANDFULLTEXTINDEXSCAN_H_
#define GRAPH_OPTIMIZER_RULE_MERGELIMITANDFULLTEXTINDEXSCAN_H_

#include "graph/optimizer/OptRule.h"

namespace nebula {
namespace opt {

// Embedding limit/offset to [[FulltextIndexScan]]
// Required conditions:
// 1. Match the pattern
// Benefits:
// 1. Limit data early to optimize performance
//
// Transformation:
// Before:
//
// +--------+------------+
// | Limit |
// | (count=3, offset=1) |
// +--------+------------+
// |
// +---------+------------+
// | GetVertices/GetEdges |
// | (limit=4) |
// +---------+------------+
// |
// +---------+---------+
// | FulltextIndexScan |
// +---------+---------+
//
// After:
//
// +---------+------------+
// | GetVertices/GetEdges |
// | (limit=4) |
// +---------+------------+
// |
// +---------+-----------+
// | FulltextIndexScan |
// | (limit=4, offset=1) |
// +---------+-----------+

class MergeLimitAndFulltextIndexScanRule final : public OptRule {
public:
const Pattern &pattern() const override;

bool match(OptContext *ctx, const MatchedResult &matched) const override;
StatusOr<OptRule::TransformResult> transform(OptContext *ctx,
const MatchedResult &matched) const override;

std::string toString() const override;

private:
MergeLimitAndFulltextIndexScanRule();

static std::unique_ptr<OptRule> kInstance;
};

} // namespace opt
} // namespace nebula

#endif
4 changes: 3 additions & 1 deletion src/graph/planner/plan/Query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1058,12 +1058,14 @@ PlanNode* PatternApply::clone() const {
PlanNode* FulltextIndexScan::clone() const {
auto ret = FulltextIndexScan::make(qctx_, searchExpr_, isEdge_);
ret->cloneMembers(*this);
ret->setOffset(offset_);
return ret;
}

std::unique_ptr<PlanNodeDescription> FulltextIndexScan::explain() const {
auto desc = Explore::explain();
addDescription("isEdge", folly::to<string>(isEdge_), desc.get());
addDescription("isEdge", folly::to<std::string>(isEdge_), desc.get());
addDescription("offset", folly::to<std::string>(offset_), desc.get());
addDescription("searchExpr", searchExpr_->toString(), desc.get());
return desc;
}
Expand Down
9 changes: 9 additions & 0 deletions src/graph/planner/plan/Query.h
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,14 @@ class FulltextIndexScan : public Explore {
return isEdge_;
}

int64_t offset() const {
return offset_;
}

void setOffset(int64_t offset) {
offset_ = offset;
}

PlanNode* clone() const override;

std::unique_ptr<PlanNodeDescription> explain() const override;
Expand All @@ -779,6 +787,7 @@ class FulltextIndexScan : public Explore {

TextSearchExpression* searchExpr_{nullptr};
bool isEdge_{false};
int64_t offset_{-1};
};

// Scan vertices
Expand Down