From 1aba6d4620408f01b98f143cd0c08711c3b8afcd Mon Sep 17 00:00:00 2001 From: Huang Wei Date: Fri, 14 Jul 2023 17:43:23 +0800 Subject: [PATCH 1/3] fix: api server write json --- src/apiserver/api_server_impl.cc | 4 +-- src/apiserver/api_server_test.cc | 60 ++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/apiserver/api_server_impl.cc b/src/apiserver/api_server_impl.cc index 27f6b9c3d30..81e6af58d82 100644 --- a/src/apiserver/api_server_impl.cc +++ b/src/apiserver/api_server_impl.cc @@ -942,12 +942,12 @@ void WriteValue(JsonWriter& ar, std::shared_ptr rs, in case hybridse::sdk::kTypeBool: { bool value = false; rs->GetBool(i, &value); - ar&(value ? "true" : "false"); + ar& value; break; } default: { LOG(ERROR) << "Invalid Column Type"; - ar & "NA"; + ar & std::string("NA"); break; } } diff --git a/src/apiserver/api_server_test.cc b/src/apiserver/api_server_test.cc index f170834853f..efe62cc0dac 100644 --- a/src/apiserver/api_server_test.cc +++ b/src/apiserver/api_server_test.cc @@ -681,6 +681,66 @@ TEST_F(APIServerTest, procedure) { ASSERT_TRUE(env->cluster_remote->ExecuteDDL(env->db, "drop table trans;", &status)); } +TEST_F(APIServerTest, testResultType) { + // check about json write + const auto env = APIServerTestEnv::Instance(); + // don't need table data + std::string ddl = + "create table if not exists multi_type(c1 bool, c2 smallint, c3 int, c4 bigint, c5 float, c6 double, c7 " + "string, c8 date, c9 timestamp, c10_str string);"; + hybridse::sdk::Status status; + + env->cluster_remote->ExecuteSQL(env->db, ddl, &status); + ASSERT_TRUE(status.IsOK()) << status.msg; + ASSERT_TRUE(env->cluster_sdk->Refresh()); + std::string deploy = "deploy d1 select c1, c2, c3, c4, c5, c6, c7, c8, c9, c10_str from multi_type;"; + env->cluster_remote->ExecuteSQL(env->db, deploy, &status); + ASSERT_TRUE(status.IsOK()) << status.msg; + ASSERT_TRUE(env->cluster_sdk->Refresh()); + + { + brpc::Controller cntl; + cntl.http_request().set_method(brpc::HTTP_METHOD_POST); + cntl.http_request().uri() = "http://127.0.0.1:8010/dbs/" + env->db + "/deployments/" + "d1"; + cntl.request_attachment().append(R"({ + "input": + [ + [ true,32767,65535,9223372036854775807,1.1,2.2,"abc","2021-12-01",11111111111111,"def" ], + [ false,32767,65535,null,1.1,2.2,"abc",null,null,"def" ] + ] + })"); // null must be lower case + env->http_channel.CallMethod(NULL, &cntl, NULL, NULL, NULL); + ASSERT_FALSE(cntl.Failed()) << cntl.ErrorText(); + + LOG(INFO) << "exec deployment resp:\n" << cntl.response_attachment().to_string(); + butil::rapidjson::Document document; + // check resp data + if (document.Parse(cntl.response_attachment().to_string().c_str()).HasParseError()) { + ASSERT_TRUE(false) << "response parse failed with code " << document.GetParseError() + << ", raw resp: " << cntl.response_attachment().to_string(); + } + ASSERT_EQ(0, document["code"].GetInt()); + ASSERT_STREQ("ok", document["msg"].GetString()); + ASSERT_TRUE(document["data"].FindMember("schema") == document["data"].MemberEnd()); + ASSERT_EQ(2, document["data"]["data"].Size()); + ASSERT_EQ(0, document["data"]["common_cols_data"].Size()); + + // check data.data 2 results + auto& data = document["data"]["data"]; + ASSERT_TRUE(data[0].IsArray()); + ASSERT_TRUE(data[0][0].GetBool()); + ASSERT_EQ(data[0][2].GetInt(), 65535); + ASSERT_STREQ(data[0][7].GetString(), "2021-12-1"); + + ASSERT_TRUE(data[1].IsArray()); + ASSERT_FALSE(data[1][0].GetBool()); + ASSERT_TRUE(data[1][3].IsNull()); + } + + env->cluster_remote->ExecuteSQL(env->db, "drop deployment d1", &status); + ASSERT_TRUE(status.IsOK()) << status.msg; +} + TEST_F(APIServerTest, no_common) { const auto env = APIServerTestEnv::Instance(); From 4653aa1e69b8496dac17c43f1b86c7fcfb5763ac Mon Sep 17 00:00:00 2001 From: Huang Wei Date: Fri, 14 Jul 2023 17:47:14 +0800 Subject: [PATCH 2/3] format --- src/apiserver/api_server_impl.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/apiserver/api_server_impl.cc b/src/apiserver/api_server_impl.cc index 81e6af58d82..c24b76c40ce 100644 --- a/src/apiserver/api_server_impl.cc +++ b/src/apiserver/api_server_impl.cc @@ -947,7 +947,7 @@ void WriteValue(JsonWriter& ar, std::shared_ptr rs, in } default: { LOG(ERROR) << "Invalid Column Type"; - ar & std::string("NA"); + ar& std::string("NA"); break; } } From acc5c9fc56cae60440ce3071111bf6f47a8262a8 Mon Sep 17 00:00:00 2001 From: Huang Wei Date: Fri, 14 Jul 2023 17:49:18 +0800 Subject: [PATCH 3/3] format --- src/apiserver/api_server_test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/apiserver/api_server_test.cc b/src/apiserver/api_server_test.cc index efe62cc0dac..d14037ae506 100644 --- a/src/apiserver/api_server_test.cc +++ b/src/apiserver/api_server_test.cc @@ -708,7 +708,7 @@ TEST_F(APIServerTest, testResultType) { [ true,32767,65535,9223372036854775807,1.1,2.2,"abc","2021-12-01",11111111111111,"def" ], [ false,32767,65535,null,1.1,2.2,"abc",null,null,"def" ] ] - })"); // null must be lower case + })"); // null must be lower case env->http_channel.CallMethod(NULL, &cntl, NULL, NULL, NULL); ASSERT_FALSE(cntl.Failed()) << cntl.ErrorText();