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

Specify the floating point accuracy of the round() function #3178

Merged
merged 10 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from 7 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
21 changes: 16 additions & 5 deletions src/common/function/FunctionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ std::unordered_map<std::string, std::vector<TypeSignature>> FunctionManager::typ
TypeSignature({Value::Type::FLOAT}, Value::Type::FLOAT)}},
{"round",
{TypeSignature({Value::Type::INT}, Value::Type::FLOAT),
TypeSignature({Value::Type::FLOAT}, Value::Type::FLOAT)}},
TypeSignature({Value::Type::INT, Value::Type::INT}, Value::Type::FLOAT),
TypeSignature({Value::Type::FLOAT}, Value::Type::FLOAT),
TypeSignature({Value::Type::FLOAT, Value::Type::INT}, Value::Type::FLOAT)}},
{"sqrt",
{TypeSignature({Value::Type::INT}, Value::Type::FLOAT),
TypeSignature({Value::Type::FLOAT}, Value::Type::FLOAT)}},
Expand Down Expand Up @@ -540,17 +542,26 @@ FunctionManager::FunctionManager() {
// to nearest integral (as a floating-point value)
auto &attr = functions_["round"];
attr.minArity_ = 1;
attr.maxArity_ = 1;
attr.maxArity_ = 2;
attr.isPure_ = true;
attr.body_ = [](const auto &args) -> Value {
switch (args[0].get().type()) {
case Value::Type::NULLVALUE: {
return Value::kNullValue;
}
case Value::Type::INT: {
return std::round(args[0].get().getInt());
}
case Value::Type::INT:
case Value::Type::FLOAT: {
if (args.size() == 2) {
switch (args[1].get().type()) {
jackwener marked this conversation as resolved.
Show resolved Hide resolved
case Value::Type::INT: {
auto decimal = args[1].get().getInt();
return std::round(args[0].get().getFloat() * pow(10, decimal)) / pow(10, decimal);
}
default: {
return Value::kNullBadType;
}
}
}
return std::round(args[0].get().getFloat());
}
default: {
Expand Down
18 changes: 18 additions & 0 deletions src/common/function/test/FunctionManagerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ std::unordered_map<std::string, std::vector<Value>> FunctionManagerTest::args_ =
{"one", {-1.2}},
{"two", {2, 4}},
{"pow", {2, 3}},
{"round1", {11111.11111, 2}},
{"round2", {11111.11111, -1}},
{"round3", {11111.11111, -5}},
{"radians", {180}},
{"range1", {1, 5}},
{"range2", {1, 5, 2}},
Expand Down Expand Up @@ -269,6 +272,11 @@ TEST_F(FunctionManagerTest, functionCall) {
TEST_FUNCTION(log, args_["int"], std::log(4));
TEST_FUNCTION(log2, args_["int"], 2.0);
}
{
TEST_FUNCTION(round, args_["round1"], 11111.11);
TEST_FUNCTION(round, args_["round2"], 11110.0);
TEST_FUNCTION(round, args_["round3"], 0.0);
}
{
TEST_FUNCTION(range, args_["range1"], Value(List({1, 2, 3, 4, 5})));
TEST_FUNCTION(range, args_["range2"], Value(List({1, 3, 5})));
Expand Down Expand Up @@ -917,11 +925,21 @@ TEST_F(FunctionManagerTest, returnType) {
ASSERT_TRUE(result.ok());
EXPECT_EQ(result.value(), Value::Type::FLOAT);
}
{
auto result = FunctionManager::getReturnType("round", {Value::Type::INT, Value::Type::INT});
ASSERT_TRUE(result.ok());
EXPECT_EQ(result.value(), Value::Type::FLOAT);
}
{
auto result = FunctionManager::getReturnType("round", {Value::Type::FLOAT});
ASSERT_TRUE(result.ok());
EXPECT_EQ(result.value(), Value::Type::FLOAT);
}
{
auto result = FunctionManager::getReturnType("round", {Value::Type::FLOAT, Value::Type::INT});
ASSERT_TRUE(result.ok());
EXPECT_EQ(result.value(), Value::Type::FLOAT);
}
{
auto result = FunctionManager::getReturnType("cbrt", {Value::Type::INT});
ASSERT_TRUE(result.ok());
Expand Down
30 changes: 30 additions & 0 deletions tests/tck/features/expression/FunctionCall.feature
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,36 @@ Feature: Function Call Expression
| result |
| NULL |

Scenario: round
When executing query:
"""
YIELD round(1.1111111111, 8) as result
"""
Then the result should be, in any order:
| result |
| 1.11111111 |
When executing query:
"""
YIELD round(1111.111111, 0) as result
"""
Then the result should be, in any order:
| result |
| 1111.0 |
When executing query:
"""
YIELD round(1111.111111, -3) as result
"""
Then the result should be, in any order:
| result |
| 1000.0 |
When executing query:
"""
YIELD round(1111.111111, -4) as result
"""
Then the result should be, in any order:
| result |
| 0.0 |

jackwener marked this conversation as resolved.
Show resolved Hide resolved
Scenario: error check
When executing query:
"""
Expand Down