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 coalesce bug #3653

Merged
merged 7 commits into from
Jan 7, 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
29 changes: 6 additions & 23 deletions src/common/function/FunctionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ std::unordered_map<std::string, Value::Type> FunctionManager::variadicFunReturnT
{"concat", Value::Type::STRING},
{"concat_ws", Value::Type::STRING},
{"cos_similarity", Value::Type::FLOAT},
{"coalesce", Value::Type::__EMPTY__},
};

std::unordered_map<std::string, std::vector<TypeSignature>> FunctionManager::typeSignature_ = {
Expand Down Expand Up @@ -283,10 +284,6 @@ std::unordered_map<std::string, std::vector<TypeSignature>> FunctionManager::typ
{
TypeSignature({Value::Type::LIST}, Value::Type::__EMPTY__),
}},
{"coalesce",
{
TypeSignature({Value::Type::LIST}, Value::Type::__EMPTY__),
}},
{"range",
{TypeSignature({Value::Type::INT, Value::Type::INT}, Value::Type::LIST),
TypeSignature({Value::Type::INT, Value::Type::INT, Value::Type::INT}, Value::Type::LIST)}},
Expand Down Expand Up @@ -2092,29 +2089,15 @@ FunctionManager::FunctionManager() {
{
auto &attr = functions_["coalesce"];
attr.minArity_ = 1;
attr.maxArity_ = 1;
attr.maxArity_ = INT64_MAX;
jackwener marked this conversation as resolved.
Show resolved Hide resolved
attr.isPure_ = true;
attr.body_ = [](const auto &args) -> Value {
switch (args[0].get().type()) {
case Value::Type::NULLVALUE: {
return Value::kNullValue;
}
case Value::Type::LIST: {
auto &list = args[0].get().getList();
if (list.values.empty()) {
return Value::kNullValue;
}
for (auto &i : list.values) {
if (i != Value::kNullValue) {
return i;
}
}
return Value::kNullValue;
}
default: {
return Value::kNullBadType;
for (size_t i = 0; i < args.size(); ++i) {
if (args[i].get().type() != Value::Type::NULLVALUE) {
return args[i].get();
}
}
return Value::kNullValue;
};
}
{
Expand Down
11 changes: 7 additions & 4 deletions src/common/function/test/FunctionManagerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1648,26 +1648,29 @@ TEST_F(FunctionManagerTest, ScalarFunctionTest) {
TEST_FUNCTION(last, args_["nullvalue"], Value::kNullValue);
TEST_FUNCTION(coalesce, args_["nullvalue"], Value::kNullValue);

TEST_FUNCTION(coalesce, args_["range1"], 1);
TEST_FUNCTION(coalesce, args_["float"], 1.1);

std::vector<Value> args;
List list;
list.values.emplace_back(Value::kNullValue);
args.push_back(list);
args.emplace_back(list);

TEST_FUNCTION(head, args, Value::kNullValue);
TEST_FUNCTION(last, args, Value::kNullValue);
TEST_FUNCTION(coalesce, args, Value::kNullValue);
TEST_FUNCTION(coalesce, args, list);

list.values.insert(list.values.begin(), "head");
args[0] = list;
TEST_FUNCTION(head, args, "head");
TEST_FUNCTION(last, args, Value::kNullValue);
TEST_FUNCTION(coalesce, args, "head");
TEST_FUNCTION(coalesce, args, list);

list.values.emplace_back("last");
args[0] = list;
TEST_FUNCTION(head, args, "head")
TEST_FUNCTION(last, args, "last");
TEST_FUNCTION(coalesce, args, "head");
TEST_FUNCTION(coalesce, args, list);
}
{
// length(null) return null
Expand Down
41 changes: 41 additions & 0 deletions tests/tck/features/function/coalesce.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Feature: Fetch Int Vid Edges

Background:
Test coalesce function

Scenario: test normal case
When executing query:
"""
RETURN coalesce(null,1) as result;
"""
Then the result should be, in any order:
| result |
| 1 |
When executing query:
"""
RETURN coalesce(1,2,3) as result;
"""
Then the result should be, in any order:
| result |
| 1 |
When executing query:
"""
RETURN coalesce(null) as result;
"""
Then the result should be, in any order:
| result |
| NULL |
When executing query:
"""
RETURN coalesce(null,[1,2,3]) as result;
"""
Then the result should be, in any order:
| result |
| [1, 2, 3] |
When executing query:
"""
RETURN coalesce(null,1.234) as result;
"""
Then the result should be, in any order:
| result |
| 1.234 |