Support explicit int->bool and bool->int

Reviewers: teon.banek, buda, mislav.bradac

Reviewed By: mislav.bradac

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D999
This commit is contained in:
florijan 2017-11-22 10:23:33 +01:00
parent 8bbf1af525
commit 597934203e
2 changed files with 9 additions and 2 deletions

View File

@ -180,6 +180,8 @@ TypedValue ToBoolean(const std::vector<TypedValue> &args, GraphDbAccessor &) {
return TypedValue::Null;
case TypedValue::Type::Bool:
return args[0].Value<bool>();
case TypedValue::Type::Int:
return args[0].ValueInt() != 0L;
case TypedValue::Type::String: {
auto s = utils::ToUpperCase(utils::Trim(args[0].Value<std::string>()));
if (s == "TRUE") return true;
@ -222,6 +224,8 @@ TypedValue ToInteger(const std::vector<TypedValue> &args, GraphDbAccessor &) {
switch (args[0].type()) {
case TypedValue::Type::Null:
return TypedValue::Null;
case TypedValue::Type::Bool:
return args[0].ValueBool() ? 1L : 0L;
case TypedValue::Type::Int:
return args[0];
case TypedValue::Type::Double:

View File

@ -886,13 +886,15 @@ TEST(ExpressionEvaluator, FunctionToBoolean) {
ASSERT_THROW(EvaluateFunction("TOBOOLEAN", {}), QueryRuntimeException);
ASSERT_EQ(EvaluateFunction("TOBOOLEAN", {TypedValue::Null}).type(),
TypedValue::Type::Null);
ASSERT_EQ(EvaluateFunction("TOBOOLEAN", {123}).ValueBool(), true);
ASSERT_EQ(EvaluateFunction("TOBOOLEAN", {-213}).ValueBool(), true);
ASSERT_EQ(EvaluateFunction("TOBOOLEAN", {0}).ValueBool(), false);
ASSERT_EQ(EvaluateFunction("TOBOOLEAN", {" trUE \n\t"}).Value<bool>(), true);
ASSERT_EQ(EvaluateFunction("TOBOOLEAN", {"\n\tFalsE "}).Value<bool>(), false);
ASSERT_EQ(EvaluateFunction("TOBOOLEAN", {"\n\tFALSEA "}).type(),
TypedValue::Type::Null);
ASSERT_EQ(EvaluateFunction("TOBOOLEAN", {true}).Value<bool>(), true);
ASSERT_EQ(EvaluateFunction("TOBOOLEAN", {false}).Value<bool>(), false);
ASSERT_THROW(EvaluateFunction("TOBOOLEAN", {2}), QueryRuntimeException);
}
TEST(ExpressionEvaluator, FunctionToFloat) {
@ -912,13 +914,14 @@ TEST(ExpressionEvaluator, FunctionToInteger) {
ASSERT_THROW(EvaluateFunction("TOINTEGER", {}), QueryRuntimeException);
ASSERT_EQ(EvaluateFunction("TOINTEGER", {TypedValue::Null}).type(),
TypedValue::Type::Null);
ASSERT_EQ(EvaluateFunction("TOINTEGER", {false}).Value<int64_t>(), 0);
ASSERT_EQ(EvaluateFunction("TOINTEGER", {true}).Value<int64_t>(), 1);
ASSERT_EQ(EvaluateFunction("TOINTEGER", {"\n\t3"}).Value<int64_t>(), 3);
ASSERT_EQ(EvaluateFunction("TOINTEGER", {" -3.5 \n\t"}).Value<int64_t>(), -3);
ASSERT_EQ(EvaluateFunction("TOINTEGER", {"\n\t3X "}).type(),
TypedValue::Type::Null);
ASSERT_EQ(EvaluateFunction("TOINTEGER", {-3.5}).Value<int64_t>(), -3);
ASSERT_EQ(EvaluateFunction("TOINTEGER", {3.5}).Value<int64_t>(), 3);
ASSERT_THROW(EvaluateFunction("TOINTEGER", {true}), QueryRuntimeException);
}
TEST(ExpressionEvaluator, FunctionType) {