diff --git a/include/fma-common/utils.h b/include/fma-common/utils.h index ddf57288a..ab9130849 100644 --- a/include/fma-common/utils.h +++ b/include/fma-common/utils.h @@ -252,6 +252,12 @@ inline int ExecCmdThroughPopen(const std::string& cmd, std::ostream& os) { #endif } +inline double DoubleDecimalPlaces(double d, int num) { + auto pow = std::pow(10, num); + auto i = std::round(d * pow); + return (i / pow); +} + namespace _detail { inline FILE* OpenPipe(const std::string& cmd, const char* mode, bool redirect_stderr = false) { std::string c = cmd; diff --git a/src/lgraph_api/lgraph_types.cpp b/src/lgraph_api/lgraph_types.cpp index 91cb8cb02..dc499c820 100644 --- a/src/lgraph_api/lgraph_types.cpp +++ b/src/lgraph_api/lgraph_types.cpp @@ -13,6 +13,7 @@ */ #include "lgraph/lgraph_types.h" #include "bolt/temporal.h" +#include "fma-common/utils.h" namespace lgraph_api { @@ -31,7 +32,10 @@ std::any FieldData::ToBolt() const { case FieldType::INT64: return data.int64; case FieldType::FLOAT: - return data.sp; + { + // bolt protocol does not have float type + return fma_common::DoubleDecimalPlaces(data.sp, 5); + } case FieldType::DOUBLE: return data.dp; case FieldType::STRING: diff --git a/src/server/json_convert.h b/src/server/json_convert.h index c22a42d80..e94a0c080 100644 --- a/src/server/json_convert.h +++ b/src/server/json_convert.h @@ -15,6 +15,7 @@ #pragma once #include "tools/json.hpp" #include "core/data_type.h" +#include "fma-common/utils.h" namespace lgraph_rfc { @@ -49,7 +50,9 @@ static json FieldDataToJson(const lgraph_api::FieldData& data) { } case lgraph_api::FieldType::FLOAT: { - return json(data.AsFloat()); + // https://github.com/nlohmann/json/issues/1109 + double d = fma_common::DoubleDecimalPlaces(data.AsFloat(), 5); + return json(d); } case lgraph_api::FieldType::DOUBLE: { diff --git a/test/resource/cases/suite/gql/base.result b/test/resource/cases/suite/gql/base.result index 8502666cd..efec74df2 100644 --- a/test/resource/cases/suite/gql/base.result +++ b/test/resource/cases/suite/gql/base.result @@ -42,7 +42,7 @@ RETURN n.name, label(e), m.name LIMIT 1; MATCH (n:Person)-[e:BORN_IN WHERE e.weight > 20]->(m) RETURN n.name, e.weight, m.name LIMIT 3; -[{"e.weight":20.549999237060547,"m.name":"New York","n.name":"John Williams"},{"e.weight":20.6200008392334,"m.name":"New York","n.name":"Lindsay Lohan"},{"e.weight":20.18000030517578,"m.name":"London","n.name":"Natasha Richardson"}] +[{"e.weight":20.55,"m.name":"New York","n.name":"John Williams"},{"e.weight":20.62,"m.name":"New York","n.name":"Lindsay Lohan"},{"e.weight":20.18,"m.name":"London","n.name":"Natasha Richardson"}] MATCH (n:Person WHERE n.birthyear = 1970) RETURN CASE WHEN n.birthyear > 1970 THEN n.name ELSE -1 END AS res; diff --git a/test/test_lgraph_cli.cpp b/test/test_lgraph_cli.cpp index 546265a20..bdc206b43 100644 --- a/test/test_lgraph_cli.cpp +++ b/test/test_lgraph_cli.cpp @@ -100,7 +100,7 @@ created 2 vertices, created 0 edges. created 0 vertices, created 1 edges. n -(:person {int16:16,float:1.1100000143051147,double:100.98,int8:8,string:"foo bar",int32:32,int64:64,bool:true,datetime:1493632800000000000,date:17289}) +(:person {int16:16,float:1.11,double:100.98,int8:8,string:"foo bar",int32:32,int64:64,bool:true,datetime:1493632800000000000,date:17289}) @plan Execution Plan: Produce Results @@ -120,7 +120,7 @@ Symbol: [m] type(NODE), scope(LOCAL), symbol_id(2) Symbol: [r] type(RELATIONSHIP), scope(LOCAL), symbol_id(1) p -(:person {int16:16,float:1.1100000143051147,double:100.98,int8:8,string:"foo bar",int32:32,int64:64,bool:true,datetime:1493632800000000000,date:17289})-[:is_friend {message:"hi.."}]->(:person {int16:116,float:11.109999656677246,double:1100.98,int8:18,string:"bar foo",int32:132,int64:164,bool:true,datetime:1525168800000000000,date:17654}) +(:person {int16:16,float:1.11,double:100.98,int8:8,string:"foo bar",int32:32,int64:64,bool:true,datetime:1493632800000000000,date:17289})-[:is_friend {message:"hi.."}]->(:person {int16:116,float:11.11,double:1100.98,int8:18,string:"bar foo",int32:132,int64:164,bool:true,datetime:1525168800000000000,date:17654}) )"; WriteFile(file, statements); @@ -154,7 +154,7 @@ p [""] ["created 0 vertices, created 1 edges."] ["n"] -["(:person {int16:16,float:1.1100000143051147,double:100.98,int8:8,string:\"foo bar\",int32:32,int64:64,bool:true,datetime:1493632800000000000,date:17289})"] +["(:person {int16:16,float:1.11,double:100.98,int8:8,string:\"foo bar\",int32:32,int64:64,bool:true,datetime:1493632800000000000,date:17289})"] ["@plan"] ["Execution Plan:\nProduce Results\n Project [n]\n Expand(All) [n --> m ]\n Node By Label Scan [n:person]\n"] ["r"] @@ -162,7 +162,7 @@ p ["@profile"] ["Current Pattern Graph:\nN[0] n:person (MATCHED)\nN[1] m:person (MATCHED)\nR[0 --> 1] r:{<1>: is_friend} (MATCHED)\nSymbol: [n] type(NODE), scope(LOCAL), symbol_id(0)\nSymbol: [m] type(NODE), scope(LOCAL), symbol_id(2)\nSymbol: [r] type(RELATIONSHIP), scope(LOCAL), symbol_id(1)\n"] ["p"] -["(:person {int16:16,float:1.1100000143051147,double:100.98,int8:8,string:\"foo bar\",int32:32,int64:64,bool:true,datetime:1493632800000000000,date:17289})-[:is_friend {message:\"hi..\"}]->(:person {int16:116,float:11.109999656677246,double:1100.98,int8:18,string:\"bar foo\",int32:132,int64:164,bool:true,datetime:1525168800000000000,date:17654})"] +["(:person {int16:16,float:1.11,double:100.98,int8:8,string:\"foo bar\",int32:32,int64:64,bool:true,datetime:1493632800000000000,date:17289})-[:is_friend {message:\"hi..\"}]->(:person {int16:116,float:11.11,double:1100.98,int8:18,string:\"bar foo\",int32:132,int64:164,bool:true,datetime:1525168800000000000,date:17654})"] )xx"; WriteFile(file, statements); @@ -217,11 +217,11 @@ p 1 rows -+---------------------------------------------------------------------------------------------------------------------------------------------------------+ -| n | -+---------------------------------------------------------------------------------------------------------------------------------------------------------+ -| (:person {int16:16,float:1.1100000143051147,double:100.98,int8:8,string:"foo bar",int32:32,int64:64,bool:true,datetime:1493632800000000000,date:17289}) | -+---------------------------------------------------------------------------------------------------------------------------------------------------------+ ++-------------------------------------------------------------------------------------------------------------------------------------------+ +| n | ++-------------------------------------------------------------------------------------------------------------------------------------------+ +| (:person {int16:16,float:1.11,double:100.98,int8:8,string:"foo bar",int32:32,int64:64,bool:true,datetime:1493632800000000000,date:17289}) | ++-------------------------------------------------------------------------------------------------------------------------------------------+ 1 rows @@ -259,11 +259,11 @@ p 1 rows -+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ -| p | -+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ -| (:person {int16:16,float:1.1100000143051147,double:100.98,int8:8,string:"foo bar",int32:32,int64:64,bool:true,datetime:1493632800000000000,date:17289})-[:is_friend {message:"hi.."}]->(:person {int16:116,float:11.109999656677246,double:1100.98,int8:18,string:"bar foo",int32:132,int64:164,bool:true,datetime:1525168800000000000,date:17654}) | -+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ ++--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| p | ++--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| (:person {int16:16,float:1.11,double:100.98,int8:8,string:"foo bar",int32:32,int64:64,bool:true,datetime:1493632800000000000,date:17289})-[:is_friend {message:"hi.."}]->(:person {int16:116,float:11.11,double:1100.98,int8:18,string:"bar foo",int32:132,int64:164,bool:true,datetime:1525168800000000000,date:17654}) | ++--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 rows diff --git a/test/test_rpc.cpp b/test/test_rpc.cpp index 5a3016bfc..0af3b4807 100644 --- a/test/test_rpc.cpp +++ b/test/test_rpc.cpp @@ -1757,6 +1757,23 @@ void test_cypher(lgraph::RpcClient& client) { UT_EXPECT_TRUE(ret); json_val = web::json::value::parse(str); UT_EXPECT_EQ(json_val[0]["count(n)"].as_integer(), 6); + UT_EXPECT_TRUE(ret); +} + +void test_float(lgraph::RpcClient& client) { + std::string str; + std::string test_str2; + bool ret = client.CallCypher(str, + "CALL db.createLabel('vertex', 'float_label', 'id', " + "['id', int32, false], ['float', float, true], " + "['double', double, true])"); + UT_EXPECT_TRUE(ret); + ret = client.CallCypher(str, + "CREATE (n:float_label{id:1,float:1.2,double:1.2}) RETURN n"); + UT_EXPECT_TRUE(ret); + UT_EXPECT_EQ(str, R"!([{"n":{"identity":6,"label":"float_label","properties":{"double":1.2,"float":1.2,"id":1}}}])!"); // NOLINT + ret = client.CallCypher(str, "CALL db.deleteLabel('vertex', 'float_label')"); + UT_EXPECT_TRUE(ret); } void test_gql(lgraph::RpcClient& client) { @@ -1970,6 +1987,7 @@ void* test_rpc_client(void*) { UT_LOG() << "admin user login"; { RpcClient client3("0.0.0.0:19099", "admin", "73@TuGraph"); + test_float(client3); test_cypher(client3); test_gql(client3); test_label(client3);