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 float precision display #468

Merged
merged 3 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions include/fma-common/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion src/lgraph_api/lgraph_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
#include "lgraph/lgraph_types.h"
#include "bolt/temporal.h"
#include "fma-common/utils.h"

namespace lgraph_api {

Expand All @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion src/server/json_convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#pragma once
#include "tools/json.hpp"
#include "core/data_type.h"
#include "fma-common/utils.h"

namespace lgraph_rfc {

Expand Down Expand Up @@ -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:
{
Expand Down
28 changes: 14 additions & 14 deletions test/test_lgraph_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ created 2 vertices, created 0 edges.
<SUMMARY>
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
Expand All @@ -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);
Expand Down Expand Up @@ -154,15 +154,15 @@ p
["<SUMMARY>"]
["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"]
["[:is_friend {message:\"hi..\"}]"]
["@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);
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions test/test_rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,24 @@ 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}}}])!");
ret = client.CallCypher(str, "CALL db.deleteLabel('vertex', 'float_label')");
UT_EXPECT_TRUE(ret);
}

void test_gql(lgraph::RpcClient& client) {
Expand Down Expand Up @@ -1970,6 +1988,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);
Expand Down
Loading