Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vagetablechicken committed Nov 6, 2023
1 parent a8fa130 commit 2188027
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
4 changes: 3 additions & 1 deletion docs/zh/quickstart/sdk/rest_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@

与APIServer的交互中,请求体均为JSON格式,并支持一定的扩展格式。注意以下几点:

- 传入超过整型或浮点数最大值的数值,将会解析失败,比如,double类型传入`1e1000`
- 非数值浮点数:在传入数据时,支持传入`NaN``Infinity``-Infinity`,与缩写`Inf``-Inf`(注意是unquoted的,并非字符串,也不支持其他变种写法)。在返回数据时,支持返回`NaN``Infinity``-Infinity`(不支持变种写法)。如果你需要将三者转换为null,可以配置 `write_nan_and_inf_null`
- 可以传入整型数字到浮点数,比如,`1`可被读取为double。
- float浮点数可能有精度损失,比如,`0.3`读取后将不会严格等于`0.3`,而是`0.30000000000000004`。我们不拒绝精度损失,请从业务层面考虑是否需要对此进行处理。传入超过float max但不超过double max的值,在读取后将成为`Inf`
- `true/false``null`并不支持大写,只支持小写。
- timestamp类型暂不支持传入年月日字符串,只支持传入数值,比如`1635247427000`
- date类型请传入年月日字符串,中间不要包含任何空格。
- date类型请传入**年月日字符串**,中间不要包含任何空格。

## 数据插入

Expand Down
12 changes: 7 additions & 5 deletions src/apiserver/api_server_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,14 @@ bool APIServerImpl::AppendJsonValue(const Value& v, hybridse::sdk::DataType type
return row->AppendInt64(v.GetInt64());
}
case hybridse::sdk::kTypeFloat: {
if (!v.IsNumber()) { // relax check, int can get as double
if (!v.IsNumber()) { // relax check, int can get as double and support set float NaN&Inf

Check warning on line 243 in src/apiserver/api_server_impl.cc

View check run for this annotation

Codecov / codecov/patch

src/apiserver/api_server_impl.cc#L243

Added line #L243 was not covered by tests
return false;
}
return row->AppendFloat(boost::lexical_cast<float>(v.GetDouble()));
// IEEE 754 arithmetic allows cast nan/inf to float
return row->AppendFloat(v.GetFloat());

Check warning on line 247 in src/apiserver/api_server_impl.cc

View check run for this annotation

Codecov / codecov/patch

src/apiserver/api_server_impl.cc#L247

Added line #L247 was not covered by tests
}
case hybridse::sdk::kTypeDouble: {
if (!v.IsNumber()) {
if (!v.IsLosslessDouble()) {

Check warning on line 250 in src/apiserver/api_server_impl.cc

View check run for this annotation

Codecov / codecov/patch

src/apiserver/api_server_impl.cc#L250

Added line #L250 was not covered by tests
return false;
}
return row->AppendDouble(v.GetDouble());
Expand Down Expand Up @@ -347,7 +348,7 @@ void APIServerImpl::RegisterPut() {

// json2doc, then generate an insert sql
Document document;
if (document.Parse(req_body.to_string().c_str()).HasParseError()) {
if (document.Parse<rapidjson::kParseNanAndInfFlag>(req_body.to_string().c_str()).HasParseError()) {

Check warning on line 351 in src/apiserver/api_server_impl.cc

View check run for this annotation

Codecov / codecov/patch

src/apiserver/api_server_impl.cc#L351

Added line #L351 was not covered by tests
DLOG(INFO) << "rapidjson doc parse [" << req_body.to_string().c_str() << "] failed, code "
<< document.GetParseError() << ", offset " << document.GetErrorOffset();
writer << resp.Set("Json parse failed, error code: " + std::to_string(document.GetParseError()));
Expand Down Expand Up @@ -430,8 +431,9 @@ void APIServerImpl::ExecuteProcedure(bool has_common_col, const InterfaceProvide
auto db = db_it->second;
auto sp = sp_it->second;

// TODO(hw): JsonReader can't set SQLRequestRow simply(cuz common_cols), use raw rapidjson here
Document document;
if (document.Parse(req_body.to_string().c_str()).HasParseError()) {
if (document.Parse<rapidjson::kParseNanAndInfFlag>(req_body.to_string().c_str()).HasParseError()) {

Check warning on line 436 in src/apiserver/api_server_impl.cc

View check run for this annotation

Codecov / codecov/patch

src/apiserver/api_server_impl.cc#L436

Added line #L436 was not covered by tests
writer << resp.Set("Request body json parse failed");
return;
}
Expand Down
33 changes: 27 additions & 6 deletions src/apiserver/api_server_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,31 @@ TEST_F(APIServerTest, jsonFormat) {
reader >> d_res;
ASSERT_TRUE(std::isinf(d_res));

Check warning on line 181 in src/apiserver/api_server_test.cc

View check run for this annotation

Codecov / codecov/patch

src/apiserver/api_server_test.cc#L174-L181

Added lines #L174 - L181 were not covered by tests
}
{
// float nan inf
// IEEE 754 arithmetic allows cast nan/inf to float, so GetFloat is fine
JsonReader reader("[NaN, Infinity, -Infinity]");
ASSERT_TRUE(reader);
float f_res = -1.0;
reader.StartArray();
reader >> f_res;
ASSERT_TRUE(std::isnan(f_res));
reader >> f_res;
ASSERT_TRUE(std::isinf(f_res));
reader >> f_res;
ASSERT_TRUE(std::isinf(f_res));

Check warning on line 195 in src/apiserver/api_server_test.cc

View check run for this annotation

Codecov / codecov/patch

src/apiserver/api_server_test.cc#L186-L195

Added lines #L186 - L195 were not covered by tests
// raw way for put and procedure(common cols)
f_res = -1.0;
rapidjson::Document document;
document.Parse<rapidjson::kParseNanAndInfFlag>("[NaN, Infinity, -Infinity]");
document.StartArray();
f_res = document[0].GetFloat();
ASSERT_TRUE(std::isnan(f_res));
f_res = document[1].GetFloat();
ASSERT_TRUE(std::isinf(f_res));
f_res = document[2].GetFloat();
ASSERT_TRUE(std::isinf(f_res));

Check warning on line 206 in src/apiserver/api_server_test.cc

View check run for this annotation

Codecov / codecov/patch

src/apiserver/api_server_test.cc#L197-L206

Added lines #L197 - L206 were not covered by tests
}
{ // illegal words
JsonReader reader("nan");
ASSERT_FALSE(reader);

Check warning on line 210 in src/apiserver/api_server_test.cc

View check run for this annotation

Codecov / codecov/patch

src/apiserver/api_server_test.cc#L209-L210

Added lines #L209 - L210 were not covered by tests
Expand All @@ -196,11 +221,7 @@ TEST_F(APIServerTest, jsonFormat) {
ASSERT_FALSE(reader); // get double failed
ASSERT_FLOAT_EQ(d, -1.0); // won't change

Check warning on line 222 in src/apiserver/api_server_test.cc

View check run for this annotation

Codecov / codecov/patch

src/apiserver/api_server_test.cc#L217-L222

Added lines #L217 - L222 were not covered by tests
}
// StringBuffer buffer;
// rapidjson::Writer<StringBuffer, rapidjson::UTF8<>, rapidjson::UTF8<>, rapidjson::CrtAllocator,
// rapidjson::kWriteNanAndInfFlag> writerr(buffer);
// writerr.Double(std::numeric_limits<double>::quiet_NaN());
// LOG(INFO) << buffer.GetString();

// test json writer
JsonWriter writer;

Check warning on line 226 in src/apiserver/api_server_test.cc

View check run for this annotation

Codecov / codecov/patch

src/apiserver/api_server_test.cc#L226

Added line #L226 was not covered by tests
// about double nan, inf
Expand All @@ -210,7 +231,7 @@ TEST_F(APIServerTest, jsonFormat) {
writer << nan;
writer << inf;
double ninf = -inf;
writer << ninf;
writer << ninf;
writer.EndArray();
ASSERT_STREQ("[NaN,Infinity,-Infinity]", writer.GetString());

Check warning on line 236 in src/apiserver/api_server_test.cc

View check run for this annotation

Codecov / codecov/patch

src/apiserver/api_server_test.cc#L228-L236

Added lines #L228 - L236 were not covered by tests
}
Expand Down

0 comments on commit 2188027

Please sign in to comment.