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 optimizer rule for pushing limit into the fulltext index scan #5575

Merged
merged 7 commits into from
Jun 6, 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: 3 additions & 0 deletions src/graph/optimizer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ nebula_add_library(
rule/MergeGetNbrsAndProjectRule.cpp
rule/IndexScanRule.cpp
rule/PushLimitDownGetNeighborsRule.cpp
rule/PushLimitDownGetVerticesRule.cpp
rule/PushLimitDownGetEdgesRule.cpp
rule/PushLimitDownFulltextIndexScanRule.cpp
rule/PushLimitDownExpandAllRule.cpp
rule/PushStepSampleDownGetNeighborsRule.cpp
rule/PushStepLimitDownGetNeighborsRule.cpp
Expand Down
5 changes: 3 additions & 2 deletions src/graph/optimizer/OptRule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ Pattern Pattern::create(graph::PlanNode::Kind kind, std::initializer_list<Patter
return Pattern(kind, std::move(patterns));
}

/*static*/ Pattern Pattern::create(std::initializer_list<graph::PlanNode::Kind> kinds,
std::initializer_list<Pattern> patterns) {
/*static*/
Pattern Pattern::create(std::initializer_list<graph::PlanNode::Kind> kinds,
std::initializer_list<Pattern> patterns) {
return Pattern(std::move(kinds), std::move(patterns));
}

Expand Down
80 changes: 80 additions & 0 deletions src/graph/optimizer/rule/PushLimitDownFulltextIndexScanRule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* Copyright (c) 2020 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/

#include "graph/optimizer/rule/PushLimitDownFulltextIndexScanRule.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::PlanNode;
using nebula::graph::QueryContext;

namespace nebula {
namespace opt {

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

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

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

StatusOr<OptRule::TransformResult> PushLimitDownFulltextIndexScanRule::transform(
OptContext *octx, const MatchedResult &matched) const {
auto *qctx = octx->qctx();
auto exploreGroupNode = matched.node;
auto ftGroupNode = matched.dependencies.front().node;

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

if (!graph::ExpressionUtils::isEvaluableExpr(explore->limitExpr())) {
return TransformResult::noTransform();
}
int64_t limitRows = explore->limit(qctx);
auto ftLimit = ft->limit(qctx);
if (ftLimit >= 0 && limitRows >= ftLimit) {
return TransformResult::noTransform();
}

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

auto newFt = static_cast<FulltextIndexScan *>(ft->clone());
newFt->setLimit(limitRows);
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 PushLimitDownFulltextIndexScanRule::toString() const {
return "PushLimitDownFulltextIndexScanRule";
}

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

#ifndef GRAPH_OPTIMIZER_RULE_PUSHLIMITDOWNGETEDGESRULE_H
#define GRAPH_OPTIMIZER_RULE_PUSHLIMITDOWNGETEDGESRULE_H

#include "graph/optimizer/OptRule.h"

namespace nebula {
namespace opt {

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

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

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

std::string toString() const override;

private:
PushLimitDownFulltextIndexScanRule();

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

} // namespace opt
} // namespace nebula

#endif
79 changes: 79 additions & 0 deletions src/graph/optimizer/rule/PushLimitDownGetEdgesRule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* Copyright (c) 2020 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/

#include "graph/optimizer/rule/PushLimitDownGetEdgesRule.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::GetEdges;
using nebula::graph::Limit;
using nebula::graph::PlanNode;
using nebula::graph::QueryContext;

namespace nebula {
namespace opt {

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

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

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

StatusOr<OptRule::TransformResult> PushLimitDownGetEdgesRule::transform(
OptContext *octx, const MatchedResult &matched) const {
auto *qctx = octx->qctx();
auto limitGroupNode = matched.node;
auto geGroupNode = matched.dependencies.front().node;

const auto limit = static_cast<const Limit *>(limitGroupNode->node());
const auto ge = static_cast<const GetEdges *>(geGroupNode->node());

if (!graph::ExpressionUtils::isEvaluableExpr(limit->countExpr())) {
return TransformResult::noTransform();
}
int64_t limitRows = limit->offset() + limit->count(qctx);
auto geLimit = ge->limit(qctx);
if (geLimit >= 0 && limitRows >= geLimit) {
return TransformResult::noTransform();
}

auto newLimit = static_cast<Limit *>(limit->clone());
newLimit->setOutputVar(limit->outputVar());
auto newLimitGroupNode = OptGroupNode::create(octx, newLimit, limitGroupNode->group());

auto newGe = static_cast<GetEdges *>(ge->clone());
newGe->setLimit(limitRows);
auto newGeGroup = OptGroup::create(octx);
auto newGeGroupNode = newGeGroup->makeGroupNode(newGe);

newLimitGroupNode->dependsOn(newGeGroup);
newLimit->setInputVar(newGe->outputVar());
for (auto dep : geGroupNode->dependencies()) {
newGeGroupNode->dependsOn(dep);
}

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

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

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

#ifndef GRAPH_OPTIMIZER_RULE_PUSHLIMITDOWNGETEDGESRULE_H
#define GRAPH_OPTIMIZER_RULE_PUSHLIMITDOWNGETEDGESRULE_H

#include "graph/optimizer/OptRule.h"

namespace nebula {
namespace opt {

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

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

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

std::string toString() const override;

private:
PushLimitDownGetEdgesRule();

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

} // namespace opt
} // namespace nebula

#endif
79 changes: 79 additions & 0 deletions src/graph/optimizer/rule/PushLimitDownGetVerticesRule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* Copyright (c) 2020 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/

#include "graph/optimizer/rule/PushLimitDownGetVerticesRule.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::GetVertices;
using nebula::graph::Limit;
using nebula::graph::PlanNode;
using nebula::graph::QueryContext;

namespace nebula {
namespace opt {

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

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

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

StatusOr<OptRule::TransformResult> PushLimitDownGetVerticesRule::transform(
OptContext *octx, const MatchedResult &matched) const {
auto *qctx = octx->qctx();
auto limitGroupNode = matched.node;
auto gvGroupNode = matched.dependencies.front().node;

const auto limit = static_cast<const Limit *>(limitGroupNode->node());
const auto gv = static_cast<const GetVertices *>(gvGroupNode->node());

if (!graph::ExpressionUtils::isEvaluableExpr(limit->countExpr())) {
return TransformResult::noTransform();
}
int64_t limitRows = limit->offset() + limit->count(qctx);
auto gvLimit = gv->limit(qctx);
if (gvLimit >= 0 && limitRows >= gvLimit) {
return TransformResult::noTransform();
}

auto newLimit = static_cast<Limit *>(limit->clone());
newLimit->setOutputVar(limit->outputVar());
auto newLimitGroupNode = OptGroupNode::create(octx, newLimit, limitGroupNode->group());

auto newGv = static_cast<GetVertices *>(gv->clone());
newGv->setLimit(limitRows);
auto newGvGroup = OptGroup::create(octx);
auto newGvGroupNode = newGvGroup->makeGroupNode(newGv);

newLimitGroupNode->dependsOn(newGvGroup);
newLimit->setInputVar(newGv->outputVar());
for (auto dep : gvGroupNode->dependencies()) {
newGvGroupNode->dependsOn(dep);
}

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

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

} // namespace opt
} // namespace nebula
Loading