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

Optimize filter pushdown and fix the bug in TCK tests. #5938

Merged
merged 11 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
48 changes: 35 additions & 13 deletions src/graph/planner/match/MatchSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,25 +136,32 @@ Expression* MatchSolver::makeIndexFilter(const std::string& label,
Expression::Kind::kRelGE,
};

std::vector<const Expression*> ands;
std::vector<Expression*> opnds;
auto optr = LogicalExpression::makeAnd;
auto kind = filter->kind();
if (kinds.count(kind) == 1) {
ands.emplace_back(filter);
opnds.emplace_back(filter);
} else if (kind == Expression::Kind::kLogicalAnd) {
auto* logic = static_cast<LogicalExpression*>(filter);
ExpressionUtils::pullAnds(logic);
for (auto& operand : logic->operands()) {
ands.emplace_back(operand);
}
opnds = logic->operands();
} else if (kind == Expression::Kind::kLogicalOr) {
auto* logic = static_cast<LogicalExpression*>(filter);
ExpressionUtils::pullOrs(logic);
opnds = logic->operands();
optr = LogicalExpression::makeOr;
} else {
return nullptr;
}

auto* pool = qctx->objPool();
std::vector<Expression*> relationals;
for (auto* item : ands) {
for (auto item : opnds) {
if (kinds.count(item->kind()) != 1) {
continue;
if (optr == LogicalExpression::makeAnd) {
Xscaperrr marked this conversation as resolved.
Show resolved Hide resolved
continue;
}
return nullptr;
}

auto* binary = static_cast<const BinaryExpression*>(item);
Expand All @@ -174,27 +181,42 @@ Expression* MatchSolver::makeIndexFilter(const std::string& label,
la = static_cast<const LabelAttributeExpression*>(right);
constant = static_cast<const ConstantExpression*>(left);
} else {
continue;
if (optr == LogicalExpression::makeAnd) {
continue;
}
return nullptr;
}
if (la->left()->name() != alias) {
continue;
if (optr == LogicalExpression::makeAnd) {
continue;
}
return nullptr;
}
propName = la->right()->value().getStr();
} else {
if (left->kind() == Expression::Kind::kLabelTagProperty &&
right->kind() == Expression::Kind::kConstant) {
if (!extractTagPropName(left, alias, label, &propName)) {
continue;
if (optr == LogicalExpression::makeAnd) {
continue;
}
return nullptr;
}
constant = static_cast<const ConstantExpression*>(right);
} else if (right->kind() == Expression::Kind::kLabelTagProperty &&
left->kind() == Expression::Kind::kConstant) {
if (!extractTagPropName(right, alias, label, &propName)) {
continue;
if (optr == LogicalExpression::makeAnd) {
continue;
}
return nullptr;
}
constant = static_cast<const ConstantExpression*>(left);
} else {
continue;
if (optr == LogicalExpression::makeAnd) {
continue;
}
return nullptr;
}
}

Expand All @@ -218,7 +240,7 @@ Expression* MatchSolver::makeIndexFilter(const std::string& label,

auto* root = relationals[0];
for (auto i = 1u; i < relationals.size(); i++) {
root = LogicalExpression::makeAnd(qctx->objPool(), root, relationals[i]);
root = optr(qctx->objPool(), root, relationals[i]);
}

return root;
Expand Down
10 changes: 7 additions & 3 deletions src/graph/util/OptimizerUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,10 +801,11 @@ Status OptimizerUtils::appendColHint(std::vector<IndexColumnHint>& hints,
}

Status OptimizerUtils::appendIQCtx(const std::shared_ptr<IndexItem>& index,
std::vector<IndexQueryContext>& iqctx) {
std::vector<IndexQueryContext>& iqctx,
const Expression* filter) {
IndexQueryContext ctx;
ctx.index_id_ref() = index->get_index_id();
ctx.filter_ref() = "";
ctx.filter_ref() = (filter) ? Expression::encode(*filter) : "";
iqctx.emplace_back(std::move(ctx));
return Status::OK();
}
Expand Down Expand Up @@ -940,7 +941,10 @@ Status OptimizerUtils::createIndexQueryCtx(std::vector<IndexQueryContext>& iqctx
if (index == nullptr) {
return Status::IndexNotFound("No valid index found");
}
return appendIQCtx(index, iqctx);
auto in = static_cast<const IndexScan*>(node);
auto* filter = Expression::decode(qctx->objPool(), in->queryContext().begin()->get_filter());
auto* newFilter = ExpressionUtils::rewriteParameter(filter, qctx);
return appendIQCtx(index, iqctx, newFilter);
}

} // namespace graph
Expand Down
4 changes: 3 additions & 1 deletion src/graph/util/OptimizerUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ class OptimizerUtils {
const std::vector<FilterItem> &items,
IndexQueryContextList &iqctx,
const Expression *filter = nullptr);
static Status appendIQCtx(const IndexItemPtr &index, IndexQueryContextList &iqctx);
static Status appendIQCtx(const IndexItemPtr &index,
IndexQueryContextList &iqctx,
const Expression *filter = nullptr);
static Status appendColHint(std::vector<storage::cpp2::IndexColumnHint> &hints,
const std::vector<FilterItem> &items,
const meta::cpp2::ColumnDef &col);
Expand Down
6 changes: 1 addition & 5 deletions tests/common/plan_differ.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,6 @@ def _is_subdict_nested(self, expect, resp):
# The inner map cannot be empty
if len(extracted_expected_dict) == 0:
return None
# Unnested dict, push the first key into list
if extracted_expected_dict == expect:
key_list.append(list(expect.keys())[0])

def _try_convert_json(j):
try:
res = json.loads(j)
Expand All @@ -202,7 +198,7 @@ def _try_convert_json(j):
return j

extracted_resp_dict = {}
if len(key_list) == 1:
if not key_list:
for k in resp:
extracted_resp_dict[k] = _try_convert_json(resp[k])
else:
Expand Down
101 changes: 101 additions & 0 deletions tests/tck/features/match/PushFilterDown.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
Feature: Push filter down

Background: Prepare a new space
Given an empty graph
And create a space with following options:
| partition_num | 1 |
| replica_factor | 1 |
| vid_type | FIXED_STRING(30) |
| charset | utf8 |
| collate | utf8_bin |
And having executed:
"""
CREATE tag player(name string, age int, score int, gender bool);
CREATE EDGE IF NOT EXISTS follow();
"""
And having executed:
"""
INSERT VERTEX player(name, age, score, gender) VALUES "Tim Duncan":("Tim Duncan", 42, 28, true),"Yao Ming":("Yao Ming", 38, 23, true),"Nneka Ogwumike":("Nneka Ogwumike", 35, 13, false);
INSERT EDGE follow () VALUES "Tim Duncan"->"Yao Ming":();
"""
And having executed:
"""
create tag index player_index on player();
create tag index player_name_index on player(name(8));
rebuild tag index
"""
And wait all indexes ready


Scenario: Single vertex
When profiling query:
"""
MATCH (v:player) where v.player.name == "Tim Duncan" and v.player.age > 20 RETURN v
"""
Then the execution plan should be:
| id | name | dependencies | operator info |
| 5 | Project | 4 | |
| 4 | Filter | 3 | {"condition": "((v.player.name==\"Tim Duncan\") AND (v.player.age>20))"} |
| 3 | AppendVertices | 2 | |
| 2 | IndexScan | 1 | {"indexCtx": {"filter":"((player.name==\"Tim Duncan\") AND (player.age>20))"}} |
| 1 | Start | | |
When profiling query:
"""
MATCH (v:player) where v.player.age > 20 RETURN v
"""
Then the execution plan should be:
| id | name | dependencies | operator info |
| 5 | Project | 4 | |
| 4 | Filter | 3 | {"condition": "(v.player.age>20)"} |
| 3 | AppendVertices | 2 | |
| 2 | IndexScan | 1 | {"indexCtx": {"filter":"(player.age>20)"}} |
| 1 | Start | | |
When profiling query:
"""
MATCH (v:player) where v.player.age < 3 or v.player.age > 20 RETURN v
"""
Then the execution plan should be:
| id | name | dependencies | operator info |
| 5 | Project | 4 | |
| 4 | Filter | 3 | {"condition": "((v.player.age<3) OR (v.player.age>20))"} |
| 3 | AppendVertices | 2 | |
| 2 | IndexScan | 1 | {"indexCtx": {"filter":"((player.age<3) OR (player.age>20))"}} |
| 1 | Start | | |

Scenario: Vertex and edge
When profiling query:
"""
MATCH (v:player)-[]-() where v.player.age > 20 RETURN v
"""
Then the execution plan should be:
| id | name | dependencies | operator info |
| 6 | Project | 5 | |
| 5 | Filter | 4 | |
| 4 | AppendVertices | 3 | |
| 3 | Traverse | 2 | |
| 2 | IndexScan | 1 | {"indexCtx": {"filter":"(player.age>20)"}} |
| 1 | Start | | |
When profiling query:
"""
MATCH (v:player)-[]-(o:player) where v.player.age > 20 or o.player.name == "Yao Ming" RETURN v
"""
Then the execution plan should be:
| id | name | dependencies | operator info |
| 6 | Project | 5 | |
| 5 | Filter | 4 | |
| 4 | AppendVertices | 3 | |
| 3 | Traverse | 2 | |
| 2 | IndexScan | 1 | {"indexCtx": {"filter":""}} |
| 1 | Start | | |
When profiling query:
"""
MATCH (v:player)-[]-(o:player) where v.player.age > 20 and o.player.name == "Yao Ming" RETURN v
"""
Then the execution plan should be:
| id | name | dependencies | operator info |
| 6 | Project | 5 | |
| 5 | Filter | 4 | |
| 4 | AppendVertices | 3 | |
| 3 | Traverse | 2 | |
| 2 | IndexScan | 1 | {"indexCtx": {"filter":"(player.age>20)"}} |
| 1 | Start | | |
Loading