Skip to content

Commit

Permalink
Fix coalesce bug (#3653)
Browse files Browse the repository at this point in the history
* fix coalesce

* fix test

* add test

* add tck

* fix

* fix

* fix
  • Loading branch information
jackwener committed Jan 7, 2022
1 parent 32b5ce4 commit 567c1a3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 27 deletions.
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;
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 |

0 comments on commit 567c1a3

Please sign in to comment.