Skip to content

Commit f31bfa8

Browse files
authored
Fix json float/double print format (#7572) (#7776)
1 parent aa85c35 commit f31bfa8

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

ydb/core/viewer/json_query.h

+6
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,13 @@ class TJsonQuery : public TViewerPipeClient<TJsonQuery> {
409409
}
410410

411411
TStringStream stream;
412+
constexpr ui32 doubleNDigits = std::numeric_limits<double>::max_digits10;
413+
constexpr ui32 floatNDigits = std::numeric_limits<float>::max_digits10;
414+
constexpr EFloatToStringMode floatMode = EFloatToStringMode::PREC_NDIGITS;
412415
NJson::WriteJson(&stream, &jsonResponse, {
416+
.DoubleNDigits = doubleNDigits,
417+
.FloatNDigits = floatNDigits,
418+
.FloatToStringMode = floatMode,
413419
.ValidateUtf8 = false,
414420
.WriteNanAsString = true,
415421
});

ydb/core/viewer/viewer_ut.cpp

+50
Original file line numberDiff line numberDiff line change
@@ -1590,6 +1590,56 @@ Y_UNIT_TEST_SUITE(Viewer) {
15901590
size_t AuthorizeTicketFails = 0;
15911591
};
15921592

1593+
Y_UNIT_TEST(FloatPointJsonQuery) {
1594+
TPortManager tp;
1595+
ui16 port = tp.GetPort(2134);
1596+
ui16 grpcPort = tp.GetPort(2135);
1597+
ui16 monPort = tp.GetPort(8765);
1598+
auto settings = TServerSettings(port);
1599+
settings.InitKikimrRunConfig()
1600+
.SetNodeCount(1)
1601+
.SetUseRealThreads(true)
1602+
.SetDomainName("Root")
1603+
.SetMonitoringPortOffset(monPort, true);
1604+
1605+
TServer server(settings);
1606+
server.EnableGRpc(grpcPort);
1607+
TClient client(settings);
1608+
1609+
TTestActorRuntime& runtime = *server.GetRuntime();
1610+
runtime.SetLogPriority(NKikimrServices::GRPC_SERVER, NLog::PRI_TRACE);
1611+
runtime.SetLogPriority(NKikimrServices::TICKET_PARSER, NLog::PRI_TRACE);
1612+
1613+
TKeepAliveHttpClient httpClient("localhost", monPort);
1614+
TStringStream responseStream;
1615+
TKeepAliveHttpClient::THeaders headers;
1616+
headers["Content-Type"] = "application/json";
1617+
headers["Authorization"] = "test_ydb_token";
1618+
TString requestBody = R"json({
1619+
"query": "SELECT cast('311111111113.222222223' as Double);",
1620+
"database": "/Root",
1621+
"action": "execute-script",
1622+
"syntax": "yql_v1",
1623+
"stats": "profile"
1624+
})json";
1625+
const TKeepAliveHttpClient::THttpCode statusCode = httpClient.DoPost("/viewer/query?timeout=600000&base64=false&schema=modern", requestBody, &responseStream, headers);
1626+
const TString response = responseStream.ReadAll();
1627+
UNIT_ASSERT_EQUAL_C(statusCode, HTTP_OK, statusCode << ": " << response);
1628+
{
1629+
NJson::TJsonReaderConfig jsonCfg;
1630+
1631+
NJson::TJsonValue json;
1632+
NJson::ReadJsonTree(response, &jsonCfg, &json, /* throwOnError = */ true);
1633+
1634+
auto resultSets = json["result"].GetArray();
1635+
UNIT_ASSERT_EQUAL_C(1, resultSets.size(), response);
1636+
1637+
double parsed = resultSets.begin()->GetArray().begin()->GetDouble();
1638+
double expected = 311111111113.22222;
1639+
UNIT_ASSERT_DOUBLES_EQUAL(parsed, expected, 0.00001);
1640+
}
1641+
}
1642+
15931643
Y_UNIT_TEST(AuthorizeYdbTokenWithDatabaseAttributes) {
15941644
TPortManager tp;
15951645
ui16 port = tp.GetPort(2134);

0 commit comments

Comments
 (0)