Skip to content

Commit 80fec6c

Browse files
authored
[CDC] Do not lose presition during float/double to json serialization (#7625)
1 parent 392f869 commit 80fec6c

File tree

3 files changed

+38
-36
lines changed

3 files changed

+38
-36
lines changed

ydb/core/tx/datashard/change_record_cdc_serializer.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,16 @@ class TJsonSerializer: public TBaseSerializer {
9191
friend class TChangeRecord; // used in GetPartitionKey()
9292

9393
static NJson::TJsonWriterConfig DefaultJsonConfig() {
94-
NJson::TJsonWriterConfig jsonConfig;
95-
jsonConfig.ValidateUtf8 = false;
96-
jsonConfig.WriteNanAsString = true;
97-
return jsonConfig;
94+
constexpr ui32 doubleNDigits = std::numeric_limits<double>::max_digits10;
95+
constexpr ui32 floatNDigits = std::numeric_limits<float>::max_digits10;
96+
constexpr EFloatToStringMode floatMode = EFloatToStringMode::PREC_NDIGITS;
97+
return NJson::TJsonWriterConfig {
98+
.DoubleNDigits = doubleNDigits,
99+
.FloatNDigits = floatNDigits,
100+
.FloatToStringMode = floatMode,
101+
.ValidateUtf8 = false,
102+
.WriteNanAsString = true,
103+
};
98104
}
99105

100106
protected:

ydb/tests/functional/replication/main.cpp

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33

44
#include <ydb/public/sdk/cpp/client/ydb_driver/driver.h>
55
#include <ydb/public/sdk/cpp/client/ydb_table/table.h>
6+
#include <ydb/public/sdk/cpp/client/ydb_proto/accessor.h>
67
#include <ydb/public/sdk/cpp/client/draft/ydb_scripting.h>
7-
#include <ydb/public/lib/yson_value/ydb_yson_value.h>
8-
#include <library/cpp/yson/writer.h>
98

109
#include <library/cpp/threading/local_executor/local_executor.h>
1110

@@ -14,18 +13,7 @@ using namespace NYdb::NTable;
1413

1514
namespace {
1615

17-
TString ReformatYson(const TString& yson) {
18-
TStringStream ysonInput(yson);
19-
TStringStream output;
20-
NYson::ReformatYsonStream(&ysonInput, &output, NYson::EYsonFormat::Text);
21-
return output.Str();
22-
}
23-
24-
void CompareYson(const TString& expected, const TString& actual) {
25-
UNIT_ASSERT_NO_DIFF(ReformatYson(expected), ReformatYson(actual));
26-
}
27-
28-
ui64 DoRead(TSession& s, const TString& table, ui64 expectedRows, const TString& expectedContent) {
16+
std::pair<ui64, Ydb::ResultSet> DoRead(TSession& s, const TString& table) {
2917
auto res = s.ExecuteDataQuery(
3018
Sprintf("SELECT * FROM `/local/%s`; SELECT COUNT(*) AS __count FROM `/local/%s`;",
3119
table.data(), table.data()), TTxControl::BeginTx().CommitTx()).GetValueSync();
@@ -34,23 +22,17 @@ ui64 DoRead(TSession& s, const TString& table, ui64 expectedRows, const TString&
3422
UNIT_ASSERT(rs.TryNextRow());
3523
auto count = rs.ColumnParser("__count").GetUint64();
3624

37-
if (count == expectedRows) {
38-
auto yson = NYdb::FormatResultSetYson(res.GetResultSet(0));
39-
40-
CompareYson(expectedContent, yson);
41-
}
42-
43-
return count;
25+
const auto proto = NYdb::TProtoAccessor::GetProto(res.GetResultSet(0));
26+
return {count, proto};
4427
}
4528

4629
} // namespace
4730

4831
Y_UNIT_TEST_SUITE(Replication)
4932
{
50-
Y_UNIT_TEST(UuidValue)
33+
Y_UNIT_TEST(Types)
5134
{
5235
TString connectionString = GetEnv("YDB_ENDPOINT") + "/?database=" + GetEnv("YDB_DATABASE");
53-
Cerr << connectionString << Endl;
5436
auto config = TDriverConfig(connectionString);
5537
auto driver = TDriver(config);
5638
auto tableClient = TTableClient(driver);
@@ -60,8 +42,9 @@ Y_UNIT_TEST_SUITE(Replication)
6042
auto res = session.ExecuteSchemeQuery(R"(
6143
CREATE TABLE `/local/ProducerUuidValue` (
6244
Key Uint32,
63-
Value1 Uuid,
64-
Value2 Uuid NOT NULL,
45+
v01 Uuid,
46+
v02 Uuid NOT NULL,
47+
v03 Double,
6548
PRIMARY KEY (Key)
6649
);
6750
)").GetValueSync();
@@ -74,11 +57,12 @@ Y_UNIT_TEST_SUITE(Replication)
7457
auto s = sessionResult.GetSession();
7558

7659
{
77-
const TString query = "UPSERT INTO ProducerUuidValue (Key, Value1, Value2) VALUES"
60+
const TString query = "UPSERT INTO ProducerUuidValue (Key,v01,v02,v03) VALUES"
7861
"(1, "
7962
"CAST(\"5b99a330-04ef-4f1a-9b64-ba6d5f44ea01\" as Uuid), "
80-
"UNWRAP(CAST(\"5b99a330-04ef-4f1a-9b64-ba6d5f44ea02\" as Uuid)"
81-
"));";
63+
"UNWRAP(CAST(\"5b99a330-04ef-4f1a-9b64-ba6d5f44ea02\" as Uuid)), "
64+
"CAST(\"311111111113.222222223\" as Double) "
65+
");";
8266
auto res = s.ExecuteDataQuery(query, TTxControl::BeginTx().CommitTx()).GetValueSync();
8367
UNIT_ASSERT_C(res.IsSuccess(), res.GetIssues().ToString());
8468
}
@@ -104,9 +88,22 @@ Y_UNIT_TEST_SUITE(Replication)
10488
UNIT_ASSERT_C(sessionResult.IsSuccess(), sessionResult.GetIssues().ToString());
10589

10690
auto s = sessionResult.GetSession();
107-
const TString expected = R"([[[1u];["5b99a330-04ef-4f1a-9b64-ba6d5f44ea01"];"5b99a330-04ef-4f1a-9b64-ba6d5f44ea02"]])";
91+
TUuidValue expectedV1("5b99a330-04ef-4f1a-9b64-ba6d5f44ea01");
92+
TUuidValue expectedV2("5b99a330-04ef-4f1a-9b64-ba6d5f44ea02");
93+
double expectedV3 = 311111111113.222222223;
10894
ui32 attempt = 10;
109-
while (1 != DoRead(s, "ConsumerUuidValue", 1, expected) && --attempt) {
95+
while (--attempt) {
96+
auto res = DoRead(s, "ConsumerUuidValue");
97+
if (res.first == 1) {
98+
const Ydb::ResultSet& proto = res.second;
99+
UNIT_ASSERT_VALUES_EQUAL(proto.rows(0).items(0).uint32_value(), 1);
100+
UNIT_ASSERT_VALUES_EQUAL(proto.rows(0).items(1).low_128(), expectedV1.Buf_.Halfs[0]);
101+
UNIT_ASSERT_VALUES_EQUAL(proto.rows(0).items(1).high_128(), expectedV1.Buf_.Halfs[1]);
102+
UNIT_ASSERT_VALUES_EQUAL(proto.rows(0).items(2).low_128(), expectedV2.Buf_.Halfs[0]);
103+
UNIT_ASSERT_VALUES_EQUAL(proto.rows(0).items(2).high_128(), expectedV2.Buf_.Halfs[1]);
104+
UNIT_ASSERT_DOUBLES_EQUAL(proto.rows(0).items(3).double_value(), expectedV3, 0.0001);
105+
break;
106+
}
110107
Sleep(TDuration::Seconds(1));
111108
}
112109

ydb/tests/functional/replication/ya.make

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ ENV(YDB_ERASURE=block_4-2)
66

77
PEERDIR(
88
library/cpp/threading/local_executor
9-
library/cpp/yson
109
ydb/public/sdk/cpp/client/ydb_table
10+
ydb/public/sdk/cpp/client/ydb_proto
1111
ydb/public/sdk/cpp/client/draft
12-
ydb/public/lib/yson_value
1312
)
1413

1514
SRCS(

0 commit comments

Comments
 (0)