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

Fix rewrite edge node filter. #4815

Merged
merged 3 commits into from
Dec 6, 2022
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
17 changes: 17 additions & 0 deletions src/common/function/FunctionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <folly/json.h>

#include <boost/algorithm/string/replace.hpp>
#include <cstdint>

#include "common/base/Base.h"
#include "common/datatypes/DataSet.h"
Expand Down Expand Up @@ -41,6 +42,7 @@ std::unordered_map<std::string, Value::Type> FunctionManager::variadicFunReturnT
{"concat_ws", Value::Type::STRING},
{"cos_similarity", Value::Type::FLOAT},
{"coalesce", Value::Type::__EMPTY__},
{"_any", Value::Type::__EMPTY__},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the new added _any function, is it callable from query?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understood correctly, it seems to me that you mean _all, not _any. If there are 10 edge types, all of which has the same prop: prop_x, the semantics of any could be satisfied if only 1 such prop_x is found from all 10 edge types. all means making sure and finding all prop_x from all 10 edge types. I think you mean the latter one.

};

std::unordered_map<std::string, std::vector<TypeSignature>> FunctionManager::typeSignature_ = {
Expand Down Expand Up @@ -2820,6 +2822,21 @@ FunctionManager::FunctionManager() {
}
};
}
// Get any argument which is not empty/null
{
auto &attr = functions_["_any"];
attr.minArity_ = 1;
attr.maxArity_ = INT64_MAX;
attr.isAlwaysPure_ = true;
attr.body_ = [](const auto &args) -> Value {
for (const auto &arg : args) {
if (!arg.get().isNull() && !arg.get().empty()) {
return arg.get();
}
}
return Value::kNullValue;
};
}
Comment on lines +2825 to +2839
Copy link
Contributor

@jievince jievince Dec 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think _any is very similar to the function

coalesce() | coalesce(input :: ANY?) :: (ANY?) | Returns the first non-null value in a list of expressions.

Both neo4j and nebula support it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to resuse it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should ignore EMPTY too.

} // NOLINT

// static
Expand Down
13 changes: 13 additions & 0 deletions src/common/function/test/FunctionManagerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1983,6 +1983,19 @@ TEST_F(FunctionManagerTest, PurityTest) {
ASSERT_TRUE(result.ok() && result.value() == true);
}

TEST_F(FunctionManagerTest, Any) {
auto dataset = DataSet({"col0", "col1", "col2"});
dataset.emplace_back(Row({1, true, "233"}));
dataset.emplace_back(Row({4, false, "456"}));
Value datasetValue = Value(std::move(dataset));
// null all
{ TEST_FUNCTION(_any, std::vector<Value>({Value(), Value::kNullValue}), Value::kNullBadData); }
// ok
{ TEST_FUNCTION(_any, std::vector<Value>({Value(), Value::kNullValue, Value(1)}), Value(1)); }
// only one
{ TEST_FUNCTION(_any, std::vector<Value>({Value(1)}), Value(1)); }
}

} // namespace nebula

int main(int argc, char **argv) {
Expand Down
9 changes: 4 additions & 5 deletions src/graph/optimizer/rule/PushEFilterDownRule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "graph/optimizer/rule/PushEFilterDownRule.h"

#include "common/expression/Expression.h"
#include "common/expression/FunctionCallExpression.h"
#include "graph/optimizer/OptContext.h"
#include "graph/optimizer/OptGroup.h"
#include "graph/planner/plan/PlanNode.h"
Expand Down Expand Up @@ -123,17 +124,15 @@ std::string PushEFilterDownRule::toString() const {
return nullptr;
}
} else {
ret = LogicalExpression::makeOr(pool);
std::vector<Expression *> operands;
operands.reserve(edges.size());
auto args = ArgumentList::make(pool);
for (auto &edge : edges) {
auto reEdgeExp = rewriteStarEdge(propertyExpr, spaceId, edge, schemaMng, pool);
if (reEdgeExp == nullptr) {
return nullptr;
}
operands.emplace_back(reEdgeExp);
args->addArgument(reEdgeExp);
}
static_cast<LogicalExpression *>(ret)->setOperands(std::move(operands));
ret = FunctionCallExpression::make(pool, "_any", args);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just rewrite it as like.start_year==2010 or teammate.start_year==2010?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about others?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

`like.start_year > xx' ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

`like.start_year > xx' ?

This filter could not be used in path pattern. I don't understand the purpose of the _any inner function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Storage don't support *.prop, so rewrite it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with @czpmango comment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what @Shylock-Hg wants is a generic way. In the path pattern alone, however, rewriting with == suffices.

}
return ret;
};
Expand Down
2 changes: 0 additions & 2 deletions src/graph/scheduler/AsyncMsgNotifyBasedScheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ class AsyncMsgNotifyBasedScheduler final : public Scheduler {
folly::Future<Status> execute(Executor* executor) const;

QueryContext* qctx_{nullptr};
// used for debugging when core on runtime
std::string query_;
};
} // namespace graph
} // namespace nebula
Expand Down
16 changes: 16 additions & 0 deletions tests/tck/features/bugfix/RewriteEdgeFilter.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) 2022 vesoft inc. All rights reserved.
#
# This source code is licensed under Apache 2.0 License.
Feature: Test match used in pipe

Background:
Given a graph with space named "nba"

Scenario: Order by after match
When executing query:
"""
match (v)-[e:like|teammate{start_year: 2010}]->() where id(v) == 'Tim Duncan' return e
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In openCypher, this edge filter means to find these edges which contain like or teammate labels and have the property start_year equal to 2010. So I think this filter should be converted to following format in NebulaGraph:

("like" in tags(e) and e.like.start_year = 2010) or ("teammate" in tags(e) and e.teammate.start_year=2010)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not only *.prop = xxxx, but we should have an equivalent representation of *.prop of edge property consists multiple edge types.

"""
Then the result should be, in any order, with relax comparison:
| e |
| [:teammate "Tim Duncan"->"Danny Green" @0 {end_year: 2016, start_year: 2010}] |