Skip to content

Commit 27ed671

Browse files
authored
Fix json float/double print format (#7572) (#7779)
1 parent 96bcf2e commit 27ed671

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

ydb/core/viewer/json_query.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,18 @@ class TJsonQuery : public TViewerPipeClient<TJsonQuery> {
418418
}
419419

420420
TStringStream stream;
421-
NJson::TJsonWriterConfig config;
422-
config.ValidateUtf8 = false;
423-
config.WriteNanAsString = true;
424-
NJson::WriteJson(&stream, &jsonResponse, config);
421+
422+
constexpr ui32 doubleNDigits = std::numeric_limits<double>::max_digits10;
423+
constexpr ui32 floatNDigits = std::numeric_limits<float>::max_digits10;
424+
constexpr EFloatToStringMode floatMode = EFloatToStringMode::PREC_NDIGITS;
425+
NJson::WriteJson(&stream, &jsonResponse, {
426+
.DoubleNDigits = doubleNDigits,
427+
.FloatNDigits = floatNDigits,
428+
.FloatToStringMode = floatMode,
429+
.ValidateUtf8 = false,
430+
.WriteNanAsString = true,
431+
});
432+
425433
out << stream.Str();
426434

427435
ReplyAndPassAway(out);

ydb/core/viewer/viewer_ut.cpp

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,56 @@ Y_UNIT_TEST_SUITE(Viewer) {
11151115
size_t AuthorizeTicketFails = 0;
11161116
};
11171117

1118+
Y_UNIT_TEST(FloatPointJsonQuery) {
1119+
TPortManager tp;
1120+
ui16 port = tp.GetPort(2134);
1121+
ui16 grpcPort = tp.GetPort(2135);
1122+
ui16 monPort = tp.GetPort(8765);
1123+
auto settings = TServerSettings(port);
1124+
settings.InitKikimrRunConfig()
1125+
.SetNodeCount(1)
1126+
.SetUseRealThreads(true)
1127+
.SetDomainName("Root")
1128+
.SetMonitoringPortOffset(monPort, true);
1129+
1130+
TServer server(settings);
1131+
server.EnableGRpc(grpcPort);
1132+
TClient client(settings);
1133+
1134+
TTestActorRuntime& runtime = *server.GetRuntime();
1135+
runtime.SetLogPriority(NKikimrServices::GRPC_SERVER, NLog::PRI_TRACE);
1136+
runtime.SetLogPriority(NKikimrServices::TICKET_PARSER, NLog::PRI_TRACE);
1137+
1138+
TKeepAliveHttpClient httpClient("localhost", monPort);
1139+
TStringStream responseStream;
1140+
TKeepAliveHttpClient::THeaders headers;
1141+
headers["Content-Type"] = "application/json";
1142+
headers["Authorization"] = "test_ydb_token";
1143+
TString requestBody = R"json({
1144+
"query": "SELECT cast('311111111113.222222223' as Double);",
1145+
"database": "/Root",
1146+
"action": "execute-script",
1147+
"syntax": "yql_v1",
1148+
"stats": "profile"
1149+
})json";
1150+
const TKeepAliveHttpClient::THttpCode statusCode = httpClient.DoPost("/viewer/json/query?timeout=600000&base64=false&schema=modern", requestBody, &responseStream, headers);
1151+
const TString response = responseStream.ReadAll();
1152+
UNIT_ASSERT_EQUAL_C(statusCode, HTTP_OK, statusCode << ": " << response);
1153+
{
1154+
NJson::TJsonReaderConfig jsonCfg;
1155+
1156+
NJson::TJsonValue json;
1157+
NJson::ReadJsonTree(response, &jsonCfg, &json, /* throwOnError = */ true);
1158+
1159+
auto resultSets = json["result"].GetArray();
1160+
UNIT_ASSERT_EQUAL_C(1, resultSets.size(), response);
1161+
1162+
double parsed = resultSets.begin()->GetArray().begin()->GetDouble();
1163+
double expected = 311111111113.22222;
1164+
UNIT_ASSERT_DOUBLES_EQUAL(parsed, expected, 0.00001);
1165+
}
1166+
}
1167+
11181168
Y_UNIT_TEST(AuthorizeYdbTokenWithDatabaseAttributes) {
11191169
TPortManager tp;
11201170
ui16 port = tp.GetPort(2134);
@@ -1162,7 +1212,7 @@ Y_UNIT_TEST_SUITE(Viewer) {
11621212
"syntax": "yql_v1",
11631213
"stats": "profile"
11641214
})json";
1165-
const TKeepAliveHttpClient::THttpCode statusCode = httpClient.DoPost("/viewer/query?timeout=600000&base64=false&schema=modern", requestBody, &responseStream, headers);
1215+
const TKeepAliveHttpClient::THttpCode statusCode = httpClient.DoPost("/viewer/json/query?timeout=600000&base64=false&schema=modern", requestBody, &responseStream, headers);
11661216
const TString response = responseStream.ReadAll();
11671217
UNIT_ASSERT_EQUAL_C(statusCode, HTTP_OK, statusCode << ": " << response);
11681218

0 commit comments

Comments
 (0)