Skip to content
This repository has been archived by the owner on Dec 1, 2022. It is now read-only.

Commit

Permalink
Specify full index scan when using not equal as filter (#1260)
Browse files Browse the repository at this point in the history
* Specify full index scan for using != as filter and rename the optimize rules

* Fix typos
  • Loading branch information
Aiee committed Jul 16, 2021
1 parent c08b248 commit 5368115
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/optimizer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ nebula_add_library(
rule/PushFilterDownAggregateRule.cpp
rule/PushFilterDownProjectRule.cpp
rule/PushFilterDownLeftJoinRule.cpp
rule/PushFilterDownEdgeIndexScanRule.cpp
rule/PushFilterDownTagIndexScanRule.cpp
rule/OptimizeEdgeIndexScanByFilterRule.cpp
rule/OptimizeTagIndexScanByFilterRule.cpp
rule/UnionAllIndexScanBaseRule.cpp
rule/UnionAllTagIndexScanRule.cpp
rule/UnionAllEdgeIndexScanRule.cpp
Expand Down
6 changes: 6 additions & 0 deletions src/optimizer/OptimizerUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,12 @@ bool OptimizerUtils::findOptimalIndex(const Expression* condition,
std::vector<storage::cpp2::IndexColumnHint> hints;
hints.reserve(index.hints.size());
auto iter = index.hints.begin();

// Use full scan if the highest index score is NotEqual
if (iter->score == IndexScore::kNotEqual) {
return false;
}

for (; iter != index.hints.end(); ++iter) {
auto& hint = *iter;
if (hint.score == IndexScore::kPrefix) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/

#include "optimizer/rule/PushFilterDownEdgeIndexScanRule.h"
#include "optimizer/rule/OptimizeEdgeIndexScanByFilterRule.h"
#include <algorithm>
#include <memory>
#include <vector>
Expand Down Expand Up @@ -42,20 +42,20 @@ using TransformResult = nebula::opt::OptRule::TransformResult;
namespace nebula {
namespace opt {

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

PushFilterDownEdgeIndexScanRule::PushFilterDownEdgeIndexScanRule() {
OptimizeEdgeIndexScanByFilterRule::OptimizeEdgeIndexScanByFilterRule() {
RuleSet::DefaultRules().addRule(this);
}

const Pattern& PushFilterDownEdgeIndexScanRule::pattern() const {
const Pattern& OptimizeEdgeIndexScanByFilterRule::pattern() const {
static Pattern pattern =
Pattern::create(Kind::kFilter, {Pattern::create(Kind::kEdgeIndexFullScan)});
return pattern;
}

bool PushFilterDownEdgeIndexScanRule::match(OptContext* ctx, const MatchedResult& matched) const {
bool OptimizeEdgeIndexScanByFilterRule::match(OptContext* ctx, const MatchedResult& matched) const {
if (!OptRule::match(ctx, matched)) {
return false;
}
Expand Down Expand Up @@ -90,7 +90,7 @@ EdgeIndexScan* makeEdgeIndexScan(QueryContext* qctx, const EdgeIndexScan* scan,
return scanNode;
}

StatusOr<TransformResult> PushFilterDownEdgeIndexScanRule::transform(
StatusOr<TransformResult> OptimizeEdgeIndexScanByFilterRule::transform(
OptContext* ctx,
const MatchedResult& matched) const {
auto filter = static_cast<const Filter*>(matched.planNode());
Expand Down Expand Up @@ -124,8 +124,8 @@ StatusOr<TransformResult> PushFilterDownEdgeIndexScanRule::transform(
return result;
}

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

} // namespace opt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/

#ifndef OPTIMIZER_RULE_PUSHFILTERDOWNEDGEINDEXSCANRULE_H_
#define OPTIMIZER_RULE_PUSHFILTERDOWNEDGEINDEXSCANRULE_H_
#ifndef OPTIMIZER_RULE_OPTIMIZEEDGEINDEXSCANBYFILTERRULE_H_
#define OPTIMIZER_RULE_OPTIMIZEEDGEINDEXSCANBYFILTERRULE_H_

#include <memory>

Expand All @@ -17,7 +17,7 @@ namespace opt {
// At present, we do NOT split filter conditions into two parts, one part is pushed down storage
// layer and another will be leaved into filter. Because this is enough for Lookup queries. We
// will enhance this rule for general usage later, such as MATCH queries.
class PushFilterDownEdgeIndexScanRule final : public OptRule {
class OptimizeEdgeIndexScanByFilterRule final : public OptRule {
public:
const Pattern &pattern() const override;
bool match(OptContext *ctx, const MatchedResult &matched) const override;
Expand All @@ -27,12 +27,12 @@ class PushFilterDownEdgeIndexScanRule final : public OptRule {
std::string toString() const override;

private:
PushFilterDownEdgeIndexScanRule();
OptimizeEdgeIndexScanByFilterRule();

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

} // namespace opt
} // namespace nebula

#endif // OPTIMIZER_RULE_PUSHFILTERDOWNEDGEINDEXSCANRULE_H_
#endif // OPTIMIZER_RULE_OPTIMIZEEDGEINDEXSCANBYFILTERRULE_H_
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/

#include "optimizer/rule/PushFilterDownTagIndexScanRule.h"
#include "optimizer/rule/OptimizeTagIndexScanByFilterRule.h"

#include "common/expression/Expression.h"
#include "common/interface/gen-cpp2/storage_types.h"
Expand Down Expand Up @@ -33,20 +33,20 @@ using TransformResult = nebula::opt::OptRule::TransformResult;
namespace nebula {
namespace opt {

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

PushFilterDownTagIndexScanRule::PushFilterDownTagIndexScanRule() {
OptimizeTagIndexScanByFilterRule::OptimizeTagIndexScanByFilterRule() {
RuleSet::DefaultRules().addRule(this);
}

const Pattern& PushFilterDownTagIndexScanRule::pattern() const {
const Pattern& OptimizeTagIndexScanByFilterRule::pattern() const {
static Pattern pattern =
Pattern::create(Kind::kFilter, {Pattern::create(Kind::kTagIndexFullScan)});
return pattern;
}

bool PushFilterDownTagIndexScanRule::match(OptContext* ctx, const MatchedResult& matched) const {
bool OptimizeTagIndexScanByFilterRule::match(OptContext* ctx, const MatchedResult& matched) const {
if (!OptRule::match(ctx, matched)) {
return false;
}
Expand Down Expand Up @@ -82,7 +82,7 @@ TagIndexScan* makeTagIndexScan(QueryContext* qctx, const TagIndexScan* scan, boo
return tagScan;
}

StatusOr<TransformResult> PushFilterDownTagIndexScanRule::transform(
StatusOr<TransformResult> OptimizeTagIndexScanByFilterRule::transform(
OptContext* ctx,
const MatchedResult& matched) const {
auto filter = static_cast<const Filter*>(matched.planNode());
Expand Down Expand Up @@ -117,8 +117,8 @@ StatusOr<TransformResult> PushFilterDownTagIndexScanRule::transform(
return result;
}

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

} // namespace opt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/

#ifndef OPTIMIZER_RULE_PUSHFILTERDOWNTAGINDEXSCANRULE_H_
#define OPTIMIZER_RULE_PUSHFILTERDOWNTAGINDEXSCANRULE_H_
#ifndef OPTIMIZER_RULE_OPTIMIZETAGINDEXSCANBYFILTERRULE_H_
#define OPTIMIZER_RULE_OPTIMIZETAGINDEXSCANBYFILTERRULE_H_

#include <memory>

Expand All @@ -17,7 +17,7 @@ namespace opt {
// At present, we do NOT split filter conditions into two parts, one part is pushed down storage
// layer and another will be leaved into filter. Because this is enough for Lookup queries. We
// will enhance this rule for general usage later, such as MATCH queries.
class PushFilterDownTagIndexScanRule final : public OptRule {
class OptimizeTagIndexScanByFilterRule final : public OptRule {
public:
const Pattern &pattern() const override;
bool match(OptContext *ctx, const MatchedResult &matched) const override;
Expand All @@ -27,12 +27,12 @@ class PushFilterDownTagIndexScanRule final : public OptRule {
std::string toString() const override;

private:
PushFilterDownTagIndexScanRule();
OptimizeTagIndexScanByFilterRule();

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

} // namespace opt
} // namespace nebula

#endif // OPTIMIZER_RULE_PUSHFILTERDOWNTAGINDEXSCANRULE_H_
#endif // OPTIMIZER_RULE_OPTIMIZETAGINDEXSCANBYFILTERRULE_H_

0 comments on commit 5368115

Please sign in to comment.