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 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
96 changes: 57 additions & 39 deletions src/graph/planner/match/MatchSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,43 @@ bool MatchSolver::extractTagPropName(const Expression* expr,
return true;
}

bool MatchSolver::extractTagPropName(const Expression* expr,
const std::string& alias,
std::string* propName) {
if (expr->kind() != Expression::Kind::kLabelAttribute) return false;
auto laExpr = static_cast<const LabelAttributeExpression*>(expr);
if (laExpr->left()->name() != alias) return false;
*propName = laExpr->right()->value().getStr();
return true;
}

bool MatchSolver::extract(const Expression* left,
const Expression* right,
const std::string& label,
const std::string& alias,
Expression::Kind labelKind,
const ConstantExpression*& constant,
std::string& propName) {
if (left->kind() != labelKind || right->kind() != Expression::Kind::kConstant) {
return false;
}
constant = static_cast<const ConstantExpression*>(right);

return extractTagPropName(left, alias, label, &propName) ||
extractTagPropName(left, alias, &propName);
}

bool MatchSolver::extractLabelAndConstant(const Expression* left,
const Expression* right,
const std::string& label,
const std::string& alias,
Expression::Kind labelKind,
const ConstantExpression*& constant,
std::string& propName) {
return extract(left, right, label, alias, labelKind, constant, propName) ||
extract(right, left, label, alias, labelKind, constant, propName);
}

Expression* MatchSolver::makeIndexFilter(const std::string& label,
const std::string& alias,
Expression* filter,
Expand All @@ -136,25 +173,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 @@ -163,39 +207,13 @@ Expression* MatchSolver::makeIndexFilter(const std::string& label,
const ConstantExpression* constant = nullptr;
std::string propName;
// TODO(aiee) extract the logic that applies to both match and lookup
if (isEdgeProperties) {
const LabelAttributeExpression* la = nullptr;
if (left->kind() == Expression::Kind::kLabelAttribute &&
right->kind() == Expression::Kind::kConstant) {
la = static_cast<const LabelAttributeExpression*>(left);
constant = static_cast<const ConstantExpression*>(right);
} else if (right->kind() == Expression::Kind::kLabelAttribute &&
left->kind() == Expression::Kind::kConstant) {
la = static_cast<const LabelAttributeExpression*>(right);
constant = static_cast<const ConstantExpression*>(left);
} else {
continue;
}
if (la->left()->name() != alias) {
continue;
}
propName = la->right()->value().getStr();
} else {
if (left->kind() == Expression::Kind::kLabelTagProperty &&
right->kind() == Expression::Kind::kConstant) {
if (!extractTagPropName(left, alias, label, &propName)) {
continue;
}
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;
}
constant = static_cast<const ConstantExpression*>(left);
} else {
auto labelkind = (isEdgeProperties) ? Expression::Kind::kLabelAttribute
: Expression::Kind::kLabelTagProperty;
if (!extractLabelAndConstant(left, right, label, alias, labelkind, constant, propName)) {
if (optr == LogicalExpression::makeAnd) {
continue;
}
return nullptr;
}

auto* tpExpr =
Expand All @@ -218,7 +236,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
18 changes: 18 additions & 0 deletions src/graph/planner/match/MatchSolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class MatchSolver final {
QueryContext* qctx,
bool isEdgeProperties = false);

static bool extractTagPropName(const Expression* expr, const string& alias, string* propName);

static bool extractTagPropName(const Expression* expr,
const std::string& alias,
const std::string& label,
Expand All @@ -55,6 +57,22 @@ class MatchSolver final {

// Build yield columns for match & shortestPath statement
static void buildProjectColumns(QueryContext* qctx, const Path& path, SubPlan& plan);

static bool extractLabelAndConstant(const Expression* left,
const Expression* right,
const string& label,
const string& alias,
Expression::Kind labelKind,
const ConstantExpression*& constant,
string& propName);

static bool extract(const Expression* left,
const Expression* right,
const string& label,
const string& alias,
Expression::Kind labelKind,
const ConstantExpression*& constant,
string& propName);
};

} // namespace graph
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
100 changes: 100 additions & 0 deletions tests/tck/features/match/PushFilterDown.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
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 | | |
1 change: 1 addition & 0 deletions tests/tck/features/yield/parameter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Feature: Parameter
| v |
| {a: 3, b: false, c: "Tim Duncan"} |

@skip #bug to fix: https://github.com/vesoft-inc/nebula/issues/5939
Xscaperrr marked this conversation as resolved.
Show resolved Hide resolved
Scenario: [param-test-004] cypher with parameters
# where clause
When executing query:
Expand Down
Loading