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

Don't rewrite vid in list expr to or expr #5294

Merged
merged 2 commits into from
Jan 31, 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
2 changes: 1 addition & 1 deletion src/graph/planner/match/LabelIndexSeek.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ StatusOr<SubPlan> LabelIndexSeek::transformNode(NodeContext* nodeCtx) {
// refactored
auto* pool = nodeCtx->qctx->objPool();
if (nodeCtx->bindWhereClause != nullptr && nodeCtx->bindWhereClause->filter != nullptr) {
auto* filter = nodeCtx->bindWhereClause->filter;
auto* filter = ExpressionUtils::rewriteInnerInExpr(nodeCtx->bindWhereClause->filter);
const auto& nodeAlias = nodeCtx->info->alias;
const auto& schemaName = nodeCtx->scanInfo.schemaNames.back();

Expand Down
1 change: 1 addition & 0 deletions src/graph/planner/match/MatchClausePlanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "graph/planner/match/SegmentsConnector.h"
#include "graph/planner/match/ShortestPathPlanner.h"
#include "graph/planner/plan/Query.h"
#include "graph/util/ExpressionUtils.h"

namespace nebula {
namespace graph {
Expand Down
5 changes: 3 additions & 2 deletions src/graph/planner/match/PropIndexSeek.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ bool PropIndexSeek::matchNode(NodeContext* nodeCtx) {
Expression* filterInWhere = nullptr;
Expression* filterInPattern = nullptr;
if (nodeCtx->bindWhereClause != nullptr && nodeCtx->bindWhereClause->filter != nullptr) {
filterInWhere = MatchSolver::makeIndexFilter(
node.labels.back(), node.alias, nodeCtx->bindWhereClause->filter, nodeCtx->qctx);
auto* newFilter = ExpressionUtils::rewriteInnerInExpr(nodeCtx->bindWhereClause->filter);
filterInWhere =
MatchSolver::makeIndexFilter(node.labels.back(), node.alias, newFilter, nodeCtx->qctx);
}
if (!node.labelProps.empty()) {
auto props = node.labelProps.back();
Expand Down
2 changes: 1 addition & 1 deletion src/graph/planner/match/VertexIdSeek.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ bool VertexIdSeek::matchNode(NodeContext *nodeCtx) {
if (nodeVid.first == node.alias) {
nodeCtx->ids = std::move(nodeVid.second.vids);
// Eliminate Filter when it's complete predication about Vertex Id
if (ExpressionUtils::isVidPredication(nodeCtx->bindWhereClause->filter)) {
if (ExpressionUtils::isVidPredication(nodeCtx->bindWhereClause->filter, nodeCtx->qctx)) {
nodeCtx->bindWhereClause->filter = nullptr;
}
return true;
Expand Down
10 changes: 8 additions & 2 deletions src/graph/util/ExpressionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1527,7 +1527,7 @@ bool ExpressionUtils::checkExprDepth(const Expression *expr) {
return true;
}

/*static*/ bool ExpressionUtils::isVidPredication(const Expression *expr) {
/*static*/ bool ExpressionUtils::isVidPredication(const Expression *expr, QueryContext *qctx) {
if (DCHECK_NOTNULL(expr)->kind() != Expression::Kind::kRelIn &&
expr->kind() != Expression::Kind::kRelEQ) {
return false;
Expand All @@ -1543,7 +1543,13 @@ bool ExpressionUtils::checkExprDepth(const Expression *expr) {
}
if (expr->kind() == Expression::Kind::kRelIn) {
// id(V) IN [List]
if (relExpr->right()->kind() != Expression::Kind::kList) {
if (!ExpressionUtils::isEvaluableExpr(relExpr->right(), qctx)) {
return false;
}

auto rightListValue = const_cast<Expression *>(relExpr->right())
->eval(graph::QueryExpressionContext(qctx->ectx())());
if (!rightListValue.isList()) {
return false;
}
} else if (expr->kind() == Expression::Kind::kRelEQ) {
Expand Down
2 changes: 1 addition & 1 deletion src/graph/util/ExpressionUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ class ExpressionUtils {

// Whether the whole expression is vertex id predication
// e.g. id(v) == 1, id(v) IN [...]
static bool isVidPredication(const Expression* expr);
static bool isVidPredication(const Expression* expr, QueryContext* qctx);

// Check if the expr looks like `$-.e[0].likeness`
static bool isOneStepEdgeProp(const std::string& edgeAlias, const Expression* expr);
Expand Down
1 change: 0 additions & 1 deletion src/graph/validator/MatchValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,6 @@ Status MatchValidator::buildEdgeInfo(const MatchPath *path,
Status MatchValidator::validateFilter(const Expression *filter,
WhereClauseContext &whereClauseCtx) {
auto *newFilter = graph::ExpressionUtils::rewriteParameter(filter, qctx_);
newFilter = graph::ExpressionUtils::rewriteInnerInExpr(filter);
auto transformRes = ExpressionUtils::filterTransform(newFilter);
NG_RETURN_IF_ERROR(transformRes);
// rewrite Attribute to LabelTagProperty
Expand Down
11 changes: 6 additions & 5 deletions src/graph/visitor/VidExtractVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,24 @@ void VidExtractVisitor::visit(RelationalExpression *expr) {
{{label, {VidPattern::Vids::Kind::kOtherSource, {}}}}};
return;
}
if (expr->left()->kind() != Expression::Kind::kFunctionCall ||
!(expr->right()->kind() == Expression::Kind::kList ||
expr->right()->kind() == Expression::Kind::kAttribute) ||
!ExpressionUtils::isEvaluableExpr(expr->right(), qctx_)) {
if (expr->left()->kind() != Expression::Kind::kFunctionCall) {
vidPattern_ = VidPattern{};
return;
}

const auto *fCallExpr = static_cast<const FunctionCallExpression *>(expr->left());
if (fCallExpr->name() != "id" && fCallExpr->args()->numArgs() != 1 &&
fCallExpr->args()->args().front()->kind() != Expression::Kind::kLabel) {
vidPattern_ = VidPattern{};
return;
}
if (!ExpressionUtils::isEvaluableExpr(expr->right(), qctx_)) {
vidPattern_ = VidPattern{};
return;
}

auto rightListValue = expr->right()->eval(graph::QueryExpressionContext(qctx_->ectx())());
if (!rightListValue.isList()) {
vidPattern_ = VidPattern{};
return;
}
vidPattern_ = VidPattern{VidPattern::Special::kInUsed,
Expand Down
12 changes: 12 additions & 0 deletions tests/tck/features/basic/data.feature
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,15 @@ Feature: data
Then the result should be, in any order, with relax comparison:
| a | b | c |
| true | true | true |
When profiling query:
jievince marked this conversation as resolved.
Show resolved Hide resolved
"""
WITH 1 AS a WHERE a IN [2, 3, 4] RETURN a
"""
Then the result should be, in any order, with relax comparison:
| a |
And the execution plan should be:
| id | name | dependencies | operator info |
| 4 | Project | 2 | |
| 2 | Filter | 1 | {"condition": "($a IN [2,3,4])"} |
| 1 | Project | 3 | |
| 3 | Start | | |
7 changes: 4 additions & 3 deletions tests/tck/features/match/SeekById.feature
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ Feature: Match seek by id
| 11 | Project | 8 | |
| 8 | Filter | 4 | |
| 4 | AppendVertices | 10 | |
| 10 | Traverse | 1 | |
| 1 | IndexScan | 2 | |
| 2 | Start | | |
| 10 | Traverse | 2 | |
| 2 | Dedup | 4 | |
| 1 | PassThrough | 3 | |
| 3 | Start | | |
3 changes: 1 addition & 2 deletions tests/tck/features/optimizer/CasesUsingTestSpace.feature
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ Feature: Push Filter Down Cases Using the test Space
| 17 | project | 19 | |
| 19 | aggregate | 24 | |
| 24 | HashInnerJoin | 21,29 | |
| 21 | project | 20 | |
| 20 | filter | 6 | |
| 21 | project | 6 | |
| 6 | AppendVertices | 26 | |
| 26 | Traverse | 25 | |
| 25 | Traverse | 2 | |
Expand Down
53 changes: 41 additions & 12 deletions tests/tck/features/optimizer/PushFilterDownTraverseRule.feature
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ Feature: Push Filter down Traverse rule
| 80 | 42 |
| 95 | 42 |
And the execution plan should be:
| id | name | dependencies | operator info |
| 6 | Project | 5 | |
| 5 | AppendVertices | 10 | |
| 10 | Traverse | 9 | {"filter": "((like._dst==\"Tony Parker\") OR (like._dst==\"Tim Duncan\"))"} |
| 9 | IndexScan | 3 | |
| 3 | Start | | |
| id | name | dependencies | operator info |
| 6 | Project | 5 | |
| 5 | AppendVertices | 10 | |
| 10 | Traverse | 9 | {"filter": "(like._dst IN [\"Tony Parker\",\"Tim Duncan\"])"} |
| 9 | IndexScan | 3 | |
| 3 | Start | | |
# The following two match statements is equivalent, so the minus of them should be empty.
When executing query:
"""
Expand All @@ -90,12 +90,12 @@ Feature: Push Filter down Traverse rule
| 95 | 41 |
| 95 | 36 |
And the execution plan should be:
| id | name | dependencies | operator info |
| 6 | Project | 5 | |
| 5 | AppendVertices | 10 | |
| 10 | Traverse | 9 | {"filter": "((like._src==\"Tony Parker\") OR (like._src==\"Tim Duncan\"))"} |
| 9 | IndexScan | 3 | |
| 3 | Start | | |
| id | name | dependencies | operator info |
| 6 | Project | 5 | |
| 5 | AppendVertices | 10 | |
| 10 | Traverse | 9 | {"filter": "(like._src IN [\"Tony Parker\",\"Tim Duncan\"])"} |
| 9 | IndexScan | 3 | |
| 3 | Start | | |
# The following two match statements is equivalent, so the minus of them should be empty.
When executing query:
"""
Expand Down Expand Up @@ -149,6 +149,35 @@ Feature: Push Filter down Traverse rule
| 14 | Traverse | 12 | | |
| 12 | Traverse | 11 | | |
| 11 | Argument | | | |
When profiling query:
"""
MATCH (v)-[e1:like]->(v1) WHERE id(v) == "Tony Parker"
WITH DISTINCT v1, e1.degree AS strength
ORDER BY strength DESC LIMIT 20
MATCH (v1)<-[e2:like]-(v2)
WITH v1, e2, v2 WHERE none_direct_dst(e2) IN ["Yao Ming", "Tim Duncan"]
RETURN id(v2) AS candidate, count(*) AS cnt;
"""
Then the result should be, in any order, with relax comparison:
| candidate | cnt |
| "Tim Duncan" | 1 |
And the execution plan should be:
| id | name | dependencies | profiling data | operator info |
| 18 | Aggregate | 22 | | |
| 22 | Project | 25 | | |
| 25 | HashInnerJoin | 20,27 | | |
| 20 | TopN | 8 | | |
| 8 | Dedup | 19 | | |
| 19 | Project | 5 | | |
| 5 | AppendVertices | 4 | | |
| 4 | Traverse | 2 | | |
| 2 | Dedup | 1 | | |
| 1 | PassThrough | 3 | | |
| 3 | Start | | | |
| 27 | Project | 28 | | |
| 28 | AppendVertices | 30 | | |
| 30 | Traverse | 11 | | {"filter": "(like._dst IN [\"Yao Ming\",\"Tim Duncan\"])"} |
| 11 | Argument | | | |

Scenario: push filter down Traverse with complex filter
When profiling query:
Expand Down
3 changes: 1 addition & 2 deletions tests/tck/features/yield/parameter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ Feature: Parameter
| "Tony Parker" |
And the execution plan should be:
| id | name | dependencies | operator info |
| 9 | Project | 7 | |
| 7 | Filter | 2 | |
| 9 | Project | 2 | |
| 2 | AppendVertices | 6 | |
| 6 | Dedup | 6 | |
| 6 | PassThrough | 0 | |
Expand Down